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

 Live Demo

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");

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

123 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements