Related Articles

Related Articles

GregorianCalendar equals() Method in Java
  • Last Updated : 27 Jul, 2018

The java.util.GregorianCalendar.equals() method is an in-built function in Java which checks for equality between this GregorianCalendar instance and the Object passed as parameter to the function. It returns true only if the specified Object is a GregorianCalendar object with same time value (millisecond offset from the Epoch) as this GregorianCalendar instance.

Syntax:

public boolean equals(Object obj)

Parameters: The function accepts a single mandatory parameter obj which is to be compared with this GregorianCalendar instance.

Return Values: This method returns true only when the specified Object is a GregorianCalendar object and has the same time value (millisecond offset from the Epoch) as this instance and returns false otherwise.

Examples:



Input : c1 = Mon Jul 23 23:46:14 UTC 2018, c2 = Mon Jul 23 23:46:14 UTC 2018
Output : true

Input : c1 = Mon Jul 23 23:46:14 UTC 2018, c2 = Sun Jul 24 00:02:52 UTC 2022
Output : false

Below programs illustrate the java.util.GregorianCalendar.equals() function:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate the equals() function 
// of GregorianCalendar class
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Create a new calendar
        GregorianCalendar c1 = (GregorianCalendar)
                     GregorianCalendar.getInstance();
  
        // Display the current date and time
        System.out.println("Current Date and Time : "
                           + c1.getTime());
  
        // Create a second calendar equal to first one
        GregorianCalendar c2 = 
              (GregorianCalendar)(Calendar)c1.clone();
  
        // Compare the two calendars
        System.out.println("Both calendars are equal:"
                           + c1.equals(c2));
  
        // Adding 15 months to second calender
        c2.add(GregorianCalendar.MONTH, 15);
  
        // Display the current date and time
        System.out.println("Modified Date and Time : "
                           + c2.getTime());
  
        // Compare the two calendars
        System.out.println("Both calendars are equal:"
                           + c1.equals(c2));
    }
}

chevron_right


Output:

Current Date and Time : Fri Jul 27 12:05:05 UTC 2018
Both calendars are equal:true
Modified Date and Time : Sun Oct 27 12:05:05 UTC 2019
Both calendars are equal:false

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate the equals() function 
// of GregorianCalendar class
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Create a new calendar
        GregorianCalendar c1 = (GregorianCalendar)
                     GregorianCalendar.getInstance();
  
        // Display the current date and time
        System.out.println("Current Date and Time : "
                           + c1.getTime());
  
        // Create a second calendar equal to first one
        GregorianCalendar c2 = 
             (GregorianCalendar)(Calendar)c1.clone();
  
        // Compare the two calendars
        System.out.println("Both calendars are equal:"
                           + c1.equals(c2));
  
        // Changing the Time Zone of c2
        c2.setTimeZone(TimeZone.getTimeZone("CST"));
  
        // Compare the two calendars
        System.out.println("Both calendars are equal:"
                           + c1.equals(c2));
    }
}

chevron_right


Output:

Current Date and Time : Fri Jul 27 12:05:08 UTC 2018
Both calendars are equal:true
Both calendars are equal:false

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#equals()

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :