Java Date Time - Java GregorianCalendar .equals (Object obj)








Syntax

GregorianCalendar.equals(Object obj) has the following syntax.

public boolean equals(Object obj)

Example

In the following code shows how to use GregorianCalendar.equals(Object obj) method.

/*  w w  w  . j  a  v a2s.  c  o m*/

import java.util.*;

public class Main {

   public static void main(String[] args) {


      GregorianCalendar cal1 = 
              (GregorianCalendar) GregorianCalendar.getInstance();


      // 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));

   }
}

The code above generates the following result.