Java Calendar - equals() Method
The java.util.Calendar.equals() method is used to compare this Calendar to the specified Object. The result is true if the argument is a Calendar object of the same calendar system that represents the same time value (millisecond offset from the Epoch) under the same Calendar parameters as this object.
Syntax
public boolean equals(Object obj)
Parameters
obj |
Specify the object to compare with. |
Return Value
Returns true if this object is equal to obj; false otherwise.
Exception
NA
Example:
In the example below, the java.util.Calendar.equals() method is used to check the given Calendar objects for equality.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a Calendar object with specified date Calendar Cal1 = new GregorianCalendar(2015, 10, 25); Calendar Cal2 = (Calendar)Cal1.clone(); Calendar Cal3 = new GregorianCalendar(2016, 11, 12); //printing Calendars System.out.println("Cal1 is " + Cal1.getTime()); System.out.println("Cal2 is " + Cal2.getTime()); System.out.println("Cal3 is " + Cal3.getTime()); //checking Cal1 and Cal2 for equality System.out.println("Are Cal1 and Cal2 equal?: " + Cal1.equals(Cal2)); //checking Cal1 and Cal3 for equality System.out.println("Are Cal1 and Cal3 equal?: " + Cal1.equals(Cal3)); } }
The output of the above code will be:
Cal1 is Wed Nov 25 00:00:00 UTC 2015 Cal2 is Wed Nov 25 00:00:00 UTC 2015 Cal3 is Mon Dec 12 00:00:00 UTC 2016 Are Cal1 and Cal2 equal?: true Are Cal1 and Cal3 equal?: false
❮ Java.util - Calendar