Wednesday, July 3, 2024
HomeLanguagesJavaGregorianCalendar equals() Method in Java

GregorianCalendar equals() Method in Java

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: 

Java




// 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 calendar
        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));
    }
}


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:

Java




// 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));
    }
}


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()
 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments