Java 8 Clock equals() Method with Examples - GeeksforGeeks

30 Sep, 2021
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 classpublic 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 classpublic 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- 
varshagumber28
ruhelaa48
Java-Clock
Java-Functions
Java-time package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Generics in Java
Comparator Interface in Java with Examples
Strings in Java
How to remove an element from ArrayList in Java?
Difference between Abstract Class and Interface in Java