SimpleDateFormat equals() Method in Java with Examples
The equals() Method of SimpleDateFormat class is used to compare two SimpleDateFormat objects. The method returns True if this SimpleDateFormat 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 SimpleDateFormat 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 SimpleDateFormat:
Example 1:
// Java code to illustrate equals() method import java.text.*; import java.util.*; public class SimpleDateFormat_Demo { public static void main(String[] args) throws InterruptedException, ParseException { // Initializing the first formatter SimpleDateFormat SDFormat1 = new SimpleDateFormat(); // Initializing the second formatter SimpleDateFormat SDFormat2 = new SimpleDateFormat(); // Displaying both the Formats Date date = new Date(); String str_Date1 = SDFormat1.format(date); System.out.println( "First: " + (str_Date1)); String str_Date2 = SDFormat2.format(date); System.out.println( "Second: " + (str_Date2)); // Comparing both the objects System.out.println( "Equality: " + SDFormat1.equals(SDFormat2)); } } |
First: 1/30/19 9:59 AM Second: 1/30/19 9:59 AM Equality: true
Example 2:
// Java code to illustrate equals() method import java.text.*; import java.util.*; public class SimpleDateFormat_Demo { public static void main(String[] args) throws InterruptedException, ParseException { // Initializing the first formatter SimpleDateFormat SDFormat1 = new SimpleDateFormat(); // Defining the SDFormat SDFormat1.applyPattern( "MMM" ); // Initializing the second formatter SimpleDateFormat SDFormat2 = new SimpleDateFormat(); // Displaying both the Formats Date date = new Date(); String str_Date1 = SDFormat1.format(date); System.out.println("First: + (str_Date1)); String str_Date2 = SDFormat2.format(date); System.out.println( "Second: " + (str_Date2)); // Comparing both the objects System.out.println( "Equality: " + SDFormat1.equals(SDFormat2)); } } |
First: Jan Second: 1/30/19 10:00 AM Equality: false
Recommended Posts:
- SimpleDateFormat applyPattern() Method in Java with Examples
- SimpleDateFormat applyLocalizedPattern() Method in Java with Examples
- SimpleDateFormat format() Method in Java with Examples
- SimpleDateFormat toLocalizedPattern() Method in Java with Examples
- SimpleDateFormat getDateFormatSymbols() Method in Java with Examples
- SimpleDateFormat set2DigitYearStart() Method in Java with Examples
- SimpleDateFormat toPattern() Method in Java with Examples
- SimpleDateFormat setDateFormatSymbols() Method in Java with Examples
- SimpleDateFormat parse() Method in Java with Examples
- SimpleDateFormat hashCode() Method in Java with Examples
- SimpleDateFormat clone() Method in Java with Examples
- SimpleDateFormat get2DigitYearStart() Method in Java with Examples
- Set equals() method in Java with Examples
- Map equals() method in Java with Examples
- ChronoLocalDateTime 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.