Home » Java programming language

Java Calendar equals() Method with Example

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

Calendar Class equals() method

  • equals() method is available in java.util package.
  • equals() method is used to compare two Calendar objects or in other words, we can say this method is used to test the equality of this Calendar object with the given Calendar object.
  • equals() method is a non-static method, it is accessible with the class object and if we try to access the method with the class name then we will get an error.
  • equals() method does not throw an exception at the time of comparing two Calendar objects.

Syntax:

    public boolean equals(Object o);

Parameter(s):

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

Return value:

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

Example:

// Java Program to demonstrate the example of
// boolean equals(Object) method of Calendar

import java.util.*;
public class EqualsOfCalendar {
    public static void main(String[] args) {

        // Instantiating two Calendar object
        Calendar ca1 = Calendar.getInstance();
        Calendar ca2 = Calendar.getInstance();

        // By using add() method to add the 10 years
        // in ca2 to the current ca1
        ca2.add(Calendar.YEAR, 10);

        // Display ca1 and ca2
        System.out.println("ca1: " + ca1.getTime());
        System.out.println("ca2: " + ca2.getTime());

        // By using equals(Object) method is to
        // compare two calendar ca1 and ca2
        boolean status = ca1.equals(ca2);

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

Output

ca1: Fri Jan 24 12:39:22 GMT 2020
ca2: Thu Jan 24 12:39:22 GMT 2030
ca1.equals(ca2): false



Comments and Discussions!

Load comments ↻






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