Calendar.equals() method in Java
java.util.Calendar.equals() is a method in Calendar class of java.util package. The method compares this Calendar to the specified Object.The method returns true if this object is equal to object. If this is not the case, i.e, if there is any difference in the parameters between the two Calendars, then false is returned.
Syntax :
public boolean equals(Object object)
Where, object is the Object
to be compared with.
Below are some examples to understand the implementation of the Calendar.equals() function in a better way.
Example 1 :
Java
import java.util.*;
class GFG {
public static void main(String[] args)
throws InterruptedException {
Calendar cal_obj1 = Calendar.getInstance();
Calendar cal_obj2 = cal_obj1;
System.out.println( "Time 1 : " + cal_obj1.getTime());
System.out.println( "Time 2 : " + cal_obj2.getTime());
System.out.println(cal_obj1.equals(cal_obj2));
}
}
|
Output :
Time 1 : Thu Mar 01 09:36:17 UTC 2018
Time 2 : Thu Mar 01 09:36:17 UTC 2018
true
Example 2 :
Java
import java.util.*;
class GFG {
public static void main(String[] args) {
Calendar cal_obj1 = Calendar.getInstance();
Calendar cal_obj2 = Calendar.getInstance();
System.out.println( "Current date is : " +
cal_obj1.getTime());
cal_obj2.set(Calendar.YEAR, 2010 );
System.out.println( "Year is " +
cal_obj2.get(Calendar.YEAR));
System.out.println( "Result : " +
cal_obj1.equals(cal_obj2));
}
}
|
Output :
Current date is : Thu Mar 01 09:39:30 UTC 2018
Year is 2010
Result : false
Last Updated :
02 Mar, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...