java.util.Calendar.equals() is a method in Calendar class of java.util package. The method compares this Calendar to the specified Object.The method returns true if this object is equal to object. If this is not the case, i.e, if there is any difference in the parameters between the two Calendars, then false is returned.
Syntax :
public boolean equals(Object object) Where, object is the Object to be compared with.
Below are some examples to understand the implementation of the Calendar.equals() function in a better way.
Example 1 :
Java
// Java code to show the use of // equals() method of Calendar class import java.util.*; class GFG { // Driver code public static void main(String[] args) throws InterruptedException { // creating calendar object Calendar cal_obj1 = Calendar.getInstance(); Calendar cal_obj2 = cal_obj1; // printing current date System.out.println("Time 1 : " + cal_obj1.getTime()); // printing current date System.out.println("Time 2 : " + cal_obj2.getTime()); // checking if 1st date is equal to 2nd date // and printing the result System.out.println(cal_obj1.equals(cal_obj2)); } } |
Output :
Time 1 : Thu Mar 01 09:36:17 UTC 2018 Time 2 : Thu Mar 01 09:36:17 UTC 2018 true
Example 2 :
Java
// Java code to show the use of // equals() method of Calendar class import java.util.*; class GFG { // Driver code public static void main(String[] args) { // creating calendar objects Calendar cal_obj1 = Calendar.getInstance(); Calendar cal_obj2 = Calendar.getInstance(); // displaying the current date System.out.println("Current date is : " + cal_obj1.getTime()); // changing year in cal_obj2 calendar cal_obj2.set(Calendar.YEAR, 2010); // displaying the year System.out.println("Year is " + cal_obj2.get(Calendar.YEAR)); // check if calendar date is equal to current date System.out.println("Result : " + cal_obj1.equals(cal_obj2)); } } |
Output :
Current date is : Thu Mar 01 09:39:30 UTC 2018 Year is 2010 Result : false
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.lang.string.replace() method in Java
- Java.util.ArrayList.add() Method in Java
- Java.util.ArrayList.addall() method in Java
- Java.util.BitSet.set() method in Java
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java.math.BigInteger.modInverse() method in Java
- Java.math.BigInteger.probablePrime() method in Java
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Method Class | getName() Method in Java
- Method Class | toGenericString() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

