- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
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
To Continue Learning Please Login
Login with Google