Java GregorianCalendar equals() Method



Description

The Java GregorianCalendar equals() method compares this GregorianCalendar to the specified Object. The result is true if and only 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.

Declaration

Following is the declaration for java.util.GregorianCalendar.equals() method

public boolean equals(Object obj)

Parameters

obj − the object to compare with.

Return Value

This method returns true if this object is equal to obj; false otherwise.

Exception

NA

Checking Equality of Current Dated and its Cloned GregorianCalendar Instance Example

The following example shows the usage of Java Calendar equals() method. We're creating a Calendar instance of current date and a second instance is created using clone() method. We then compares them using equals() method for equality.

package com.tutorialspoint;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class GregorianCalendarDemo {
   public static void main(String[] args) {

      // create a new calendar
      GregorianCalendar cal1 = (GregorianCalendar) GregorianCalendar.getInstance();

      // print the current date and time
      System.out.println("" + cal1.getTime());

      // create a second calendar equal to first one
      GregorianCalendar cal2 = (GregorianCalendar) (Calendar) cal1.clone();

      // print cal2
      System.out.println("" + cal2.getTime());

      // compare the two calendars
      System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Fri Nov 18 10:23:47 IST 2022
Fri Nov 18 10:23:47 IST 2022
Cal1 and Cal2 are equal:true

Checking Equality of Two Different Dated GregorianCalendar Instances Example

The following example shows the usage of Java Calendar equals() method. We're creating a Calendar instance of current date and a second instance is created using clone() method and modified it by adding five years to it. We then compares them using equals() method for equality.

package com.tutorialspoint;

import java.util.*;

public class GregorianCalendarDemo {
   public static void main(String[] args) {

      // create a new calendar
      GregorianCalendar cal1 = (GregorianCalendar) GregorianCalendar.getInstance();

      // print the current date and time
      System.out.println("" + cal1.getTime());

      // create a second calendar equal to first one
      GregorianCalendar cal2 = (GregorianCalendar) (Calendar) cal1.clone();

      // print cal2
      System.out.println("" + cal2.getTime());

      // compare the two calendars
      System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));

      // change cal 2 a bit
      cal2.add(GregorianCalendar.YEAR, 5);

      // compare the two calendars
      System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Fri Nov 18 10:25:05 IST 2022
Thu Nov 18 10:25:05 IST 2027
Cal1 and Cal2 are equal:false

Checking Equality of Two Current Dated GregorianCalendar Instances Example

The following example shows the usage of Java Calendar equals() method. We're creating two Calendar instances of current date. We then compares them using equals() method for equality. As there is a difference of millisecond level, result is false.

package com.tutorialspoint;

import java.util.GregorianCalendar;

public class GregorianCalendarDemo {
   public static void main(String[] args) {

      // create a new calendar
      GregorianCalendar cal1 = (GregorianCalendar) GregorianCalendar.getInstance();

      // print the current date and time
      System.out.println("" + cal1.getTime());

      // create a second calendar
      GregorianCalendar cal2 = (GregorianCalendar) GregorianCalendar.getInstance();

      // print cal2
      System.out.println("" + cal2.getTime());
      
      // compare the two calendars
      System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Fri Nov 18 10:25:43 IST 2022
Fri Nov 18 10:25:44 IST 2022
Cal1 and Cal2 are equal:false
java_util_gregoriancalendar.htm
Advertisements