Java Utility Library

Java GregorianCalendar - equals() Method



The java.util.GregorianCalendar.equals() method is used to compare this GregorianCalendar to the specified Object. The method returns true if the argument is a GregorianCalendar object that represents the same time value (millisecond offset from the Epoch) under the same Calendar parameters and Gregorian change date 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.GregorianCalendar.equals() method is used to check the given GregorianCalendar objects for equality.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Calendar object with specified date
    GregorianCalendar Cal1 = new GregorianCalendar(2015, 10, 25);
    GregorianCalendar Cal2 = (GregorianCalendar)Cal1.clone();
    GregorianCalendar 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 GMT 2015
Cal2 is Wed Nov 25 00:00:00 GMT 2015
Cal3 is Mon Dec 12 00:00:00 GMT 2016
Are Cal1 and Cal2 equal?: true
Are Cal1 and Cal3 equal?: false

❮ Java.util - GregorianCalendar