- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java 8 Clock equals() Method
The equality of two Java clock objects can be checked using the method equals() in the Clock Class in Java. This method requires a single parameter i.e. the object that is to be compared with the existing clock object. Also it returns true if both the clock objects are equal and false otherwise.
A program that demonstrates this is given as follows −
Example
import java.time.Clock; import java.time.ZoneId; public class Demo { public static void main(String[] args) { Clock c1 = Clock.systemDefaultZone(); Clock c2 = Clock.systemDefaultZone(); System.out.println("Clock c1: " + c1.toString()); System.out.println("Clock c2: " + c2.toString()); boolean flag = c1.equals(c2); if(flag) System.out.println("
Both the clock objects are equal"); else System.out.println("
Both the clock objects are not equal"); } }
Output
Clock c1: SystemClock[Etc/UTC] Clock c2: SystemClock[Etc/UTC] Both the clock objects are equal
Now let us understand the above program.
The clock objects c1 and c2 are compared using the method equals() and the returned value is stored in flag. Then it is displayed whether the objects are equal or not. A code snippet that demonstrates this is as follows −
Clock c1 = Clock.systemDefaultZone(); Clock c2 = Clock.systemDefaultZone(); System.out.println("Clock c1: " + c1.toString()); System.out.println("Clock c2: " + c2.toString()); boolean flag = c1.equals(c2); if(flag) System.out.println("
Both the clock objects are equal"); else System.out.println("
Both the clock objects are not equal");
Advertisements