Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8.
The equals() method of java.time.Clock class checks if two Clock objects are equal or not. If clocks are equal then it returns true, else it returns false. This equals method of clock class overrides this Object.equals(java.lang.Object) method to compare based on clock object state. If not overridden, then this equal() method takes the description of java.lang.Object.equals()
Syntax:Â
public boolean equals(Object obj)
Parameters: This method takes a mandatory parameter obj which is the clock object passed to compare with the existing clock object.
Return Value: This method returns true if two clocks objects are equal. Otherwise, it returns false.
Example:Â Â
Input: Clock object of ZoneId "UTC" Clock object of ZoneId "Asia/calcutta" Output: false Explanation: Both objects represent Clock object of the different zone. Hence applying equals on them returns false.
Below programs illustrates equals() method of java.time.Clock class:
Program 1: When two similar class objects are compared.Â
Java
// Java program to demonstrate equals() // method of Clock class Â
import java.time.Clock; import java.time.ZoneId; Â
// create class public class EqualsMethodDemo { Â
    // Main method     public static void main(String[] args)     { Â
        // create clock object which represents         // UTC Zone time using system()         Clock clock1 = Clock.system(ZoneId.of( "Etc/UTC" )); Â
        // Print Clock1 details         System.out.println(clock1.toString()); Â
        // Create another class Object using         // clock class systemDefaultZone method         Clock clock2 = Clock.systemDefaultZone(); Â
        // Print Clock2 details         System.out.println(clock2.toString()); Â
        // check whether both clock objects are equal or not         boolean equalResponse = clock1.equals(clock2); Â
        // print result         System.out.println( "Both clocks are equal:"                            + equalResponse);     } } |
SystemClock[Etc/UTC] SystemClock[Etc/UTC] Both clocks are equal:true
Â
Program 2: When two different class objects are compared.
Java
// Java program to demonstrate equals() // method of Clock class Â
import java.time.Clock; import java.time.ZoneId; Â
// create class public class EqualsMethodDemo { Â
    // Main method     public static void main(String[] args)     { Â
        // Create a class Object using clock         // class systemDefaultZone method         Clock clock1 = Clock.systemDefaultZone(); Â
        // Print Clock1 Zone details         System.out.println( "clock1 Time Zone = "                            + clock1.getZone()); Â
        // Create another class Object using         // clock class systemUTC method         Clock clock2 = Clock.systemUTC(); Â
        // Print Clock2 Zone details         System.out.println( "clock2 Time Zone = "                            + clock2.getZone()); Â
        // check whether both clock objects are equal or not         boolean equalResponse = clock1.equals(clock2); Â
        // print result         System.out.println( "Both clocks are equal:"                            + equalResponse);     } } |
clock1 Time Zone = Etc/UTC clock2 Time Zone = Z Both clocks are equal:false
Â
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#equals-java.lang.Object-
Â