Java.util.GregorianCalendar.equals() Method


Advertisements


Description

The java.util.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

Example

The following example shows the usage of java.util.GregorianCalendar.equals() method.

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

   }
}

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

Fri May 18 02:40:39 EEST 2012
Fri May 18 02:40:39 EEST 2012
Cal1 and Cal2 are equal:true
Cal1 and Cal2 are equal:false

java_util_gregoriancalendar.htm

Advertisements