Java Clock Class | equals() Method with Example

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

Clock Class equals() method

  • equals() method is available in java.time package.
  • equals() method is used to check whether this Clock object and the given Object are equal or not.
  • equals() method is a non-static method, it is accessible with the class object only 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 objects.

Syntax:

    public boolean equals(Object o);

Parameter(s):

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

Return value:

The return type of this method is boolean, it returns true when both the compared objects are equal in terms of state otherwise it returns false.

Example:

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

import java.time.*;

public class EqualsOfClock {
    public static void main(String args[]) {
        // Initialize two Clock objects  
        Clock cl1 = Clock.systemDefaultZone();
        Clock cl2 = Clock.systemUTC();

        // Display cl1 instant
        Instant cl_ins = cl1.instant();
        System.out.println("cl1.instant(): " + cl_ins);

        // Display cl2 instant
        cl_ins = cl2.instant();
        System.out.println("cl2.instant(): " + cl_ins);

        // checks whether this object(cl1) is equal to
        // the given object(cl2) or not here it returns
        // false because their instant are not same
        boolean status = cl1.equals(cl2);
        System.out.println("cl1.equals (cl2): " + status);

        // checks whether this object(cl1) is equal to
        // the given object(null) or not here it returns
        // false when we compare this object
        // with null 
        status = cl1.equals(null);
        System.out.println("cl1.equals (null): " + status);
    }
}

Output

cl1.instant(): 2020-05-13T17:21:57.819316Z
cl2.instant(): 2020-05-13T17:21:57.891364Z
cl1.equals (cl2): false
cl1.equals (null): false


Comments and Discussions!

Load comments ↻





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