Java 8 Clock equals() Method with Examples
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()
public boolean equals(Object obj)
Parameters: This method takes a mandatory parameter obj which is the clock object passed to compare with existing clock object.
Return Value: This method returns true if two clocks objects are equal. Otherwise it returns false.
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.
SystemClock[Etc/UTC] SystemClock[Etc/UTC] Both clocks are equal:true
Program 2: When two different class objects are compared.
clock1 Time Zone = Etc/UTC clock2 Time Zone = Z Both clocks are equal:false
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.