DateFormat equals() Method in Java with Examples
The equals() Method of the DateFormat class is used to compare two DateFormat objects. The method returns True if this DateFormat is equal to the passed object else returns False.
Syntax:
public boolean equals(Object obj)
Parameters: The method takes one parameter obj of Object type and refers to the object that is to be compared with this DateFormat object.
Return Value: The method returns boolean True if both the objects are equal else the method returns False.
Below programs illustrate the working of equals() Method of DateFormat:
Example 1:
// Java code to illustrate equals() method import java.text.*; import java.util.*; public class DateFormat_Demo { public static void main(String[] args) { // Initializing the first formatter DateFormat DFormat1 = DateFormat.getDateInstance(); // Initializing the second formatter DateFormat DFormat2 = DateFormat.getDateInstance(); // Displaying both the Formats Date date = new Date(); String str_Date1 = DFormat1.format(date); System.out.println("First: " + (str_Date1)); String str_Date2 = DFormat2.format(date); System.out.println("Second: " + (str_Date2)); // Comparing both the objects System.out.println("Equality: " + DFormat1.equals(DFormat2)); } } |
First: Mar 27, 2019 Second: Mar 27, 2019 Equality: true
Example 2:
// Java code to illustrate equals() method import java.text.*; import java.util.*; public class DateFormat_Demo { public static void main(String[] args) { // Initializing the first formatter DateFormat DFormat1 = DateFormat.getDateInstance(); // Initializing the second formatter DateFormat DFormat2 = DateFormat.getTimeInstance(); // Displaying both the Formats Date date = new Date(); String str_Date1 = DFormat1.format(date); System.out.println("First: " + (str_Date1)); String str_Date2 = DFormat2.format(date); System.out.println("Second: " + (str_Date2)); // Comparing both the objects System.out.println("Equality: " + DFormat1.equals(DFormat2)); } } |
First: Mar 27, 2019 Second: 11:04:25 AM Equality: false
Reference: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#equals(java.lang.Object)
Recommended Posts:
- DateFormat getAvailableLocales() Method in Java with Examples
- DateFormat getCalendar() Method in Java with Examples
- DateFormat getTimeZone() Method in Java with Examples
- DateFormat getDateTimeInstance() Method in Java with Examples
- DateFormat hashCode() method in Java with Examples
- DateFormat isLenient() Method in Java with Examples
- DateFormat getTimeInstance() Method in Java with Examples
- DateFormat getDateInstance() Method in Java with Examples
- DateFormat format() Method in Java with Examples
- DateFormat setCalendar() Method in Java with Examples
- DateFormat setTimeZone() Method in Java with Examples
- DateFormat clone() method in Java with Examples
- DateFormat setLenient() Method in Java with Examples
- DateFormat parse(string , ParsePosition) Method in Java with Examples
- Map equals() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



