- 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 Calendar equals() Method
Description
The Java Calendar equals() method compares this Calendar to the specified Object.
Declaration
Following is the declaration for java.util.Calendar.equals() method
public boolean equals(Object obj)
Parameters
obj − the object to compare with.
Return Value
true if this object is equal to obj; false if there is any difference in those parameters between the two Calendars (millisecond offset from the Epoch).
Exception
NA
Comparing Different Dated GregorianCalendar instances for Equality Example
The following example shows the usage of Java Calendar equals() method. We're creating two GregorianCalendar instance of different dates. We're comparing them using equals() method and then printing the result.
package com.tutorialspoint; import java.util.Calendar; import java.util.GregorianCalendar; public class CalendarDemo { public static void main(String[] args) { // create two calendar at the different dates Calendar cal1 = new GregorianCalendar(2015, 8, 15); Calendar cal2 = new GregorianCalendar(2008, 1, 02); // compare the two calendar objects for equality. System.out.println("The result is : " + cal1.equals(cal2)); } }
Output
Let us compile and run the above program, this will produce the following result −
The result is : false
Comparing Same Dated GregorianCalendar instances for Equality Example
The following example shows the usage of Java Calendar equals() method. We're creating two GregorianCalendar instance of same date. We're comparing them using equals() method and then printing the result.
package com.tutorialspoint; import java.util.Calendar; import java.util.GregorianCalendar; public class CalendarDemo { public static void main(String[] args) { // create two calendar at the different dates Calendar cal1 = new GregorianCalendar(2015, 8, 15); Calendar cal2 = new GregorianCalendar(2015, 8, 15); // compare the two calendar objects for equality. System.out.println("The result is : " + cal1.equals(cal2)); } }
Output
Let us compile and run the above program, this will produce the following result −
The result is : true
Comparing Current Dated GregorianCalendar instances for Equality Example
The following example shows the usage of Java Calendar compareTo() method. We're creating two Calendar instance of current date and then we're comparing them using compareTo() method and then printing the result. As there is a very small difference of milliseconds, the result will come as false.
package com.tutorialspoint; import java.util.Calendar; public class CalendarDemo { public static void main(String[] args) { // create two calendar at the different dates Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); // compare the two calendar objects for equality. System.out.println("The result is : " + cal1.equals(cal2)); } }
Output
Let us compile and run the above program, this will produce the following result −
The result is : false
To Continue Learning Please Login
Login with Google