Monday, October 6, 2025
HomeLanguagesJavaCalendar.equals() method in Java

Calendar.equals() method in Java

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
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32338 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6707 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6825 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6779 POSTS0 COMMENTS