Home » Java programming language

Java GregorianCalendar equals() Method with Example

GregorianCalendar Class equals() method: Here, we are going to learn about the equals() method of GregorianCalendar Class with its syntax and example.
Submitted by Preeti Jain, on February 15, 2020

GregorianCalendar Class equals() method

  • equals() method is available in java.util package.
  • equals() method is used to check whether this GregorianCalendar and the given object (o) are equals or not.
  • equals() method is a non-static method, it is accessible with class object only and if we try to access the method with class name then we will get an error.
  • equals() method does not throw an exception at the time checking equality of this GregorianCalendar object with the given object (o).

Syntax:

    public boolean equals(Object o);

Parameter(s):

  • Object o – represents the object to be compared with this calendar.

Return value:

The return type of this method is boolean, it returns true when this GregorianCalendar is same as the given object otherwise it returns false.

Example:

// Java program to demonstrate the example 
// of boolean equals(Object o) method of 
// GregorianCalendar

import java.util.*;

public class EqualsOfGregorianCalendar {
    public static void main(String[] args) {
        // Instantiating two Calendar object
        GregorianCalendar g_ca1 = (GregorianCalendar) GregorianCalendar.getInstance();
        GregorianCalendar g_ca2 = (GregorianCalendar) GregorianCalendar.getInstance();

        // By using add() method to add the 10 years
        // in g_ca2 to the current g_ca1
        g_ca2.add(GregorianCalendar.YEAR, 10);

        // Display g_ca1 and g_ca2
        System.out.println("g_ca1: " + g_ca1.getTime());
        System.out.println("g_ca2: " + g_ca2.getTime());

        // By using equals(Object) method is to
        // compare two GregorianCalendar g_ca1 and g_ca2
        boolean status = g_ca1.equals(g_ca2);

        // Display compared result
        System.out.println("g_ca1.equals(g_ca2): " + status);
    }
}

Output

g_ca1: Sat Feb 15 07:41:32 GMT 2020
g_ca2: Fri Feb 15 07:41:32 GMT 2030
g_ca1.equals(g_ca2): false


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.