SimpleDateFormat equals() Method in Java with Examples
Last Updated :
30 Jan, 2019
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:
import java.text.*;
import java.util.*;
public class SimpleDateFormat_Demo {
public static void main(String[] args)
throws InterruptedException, ParseException
{
SimpleDateFormat SDFormat1
= new SimpleDateFormat();
SimpleDateFormat SDFormat2
= new SimpleDateFormat();
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));
System.out.println( "Equality: "
+ SDFormat1.equals(SDFormat2));
}
}
|
Output:
First: 1/30/19 9:59 AM
Second: 1/30/19 9:59 AM
Equality: true
Example 2:
import java.text.*;
import java.util.*;
public class SimpleDateFormat_Demo {
public static void main(String[] args)
throws InterruptedException, ParseException
{
SimpleDateFormat SDFormat1
= new SimpleDateFormat();
SDFormat1.applyPattern( "MMM" );
SimpleDateFormat SDFormat2
= new SimpleDateFormat();
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));
System.out.println( "Equality: "
+ SDFormat1.equals(SDFormat2));
}
}
|
Output:
First: Jan
Second: 1/30/19 10:00 AM
Equality: false
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...