Java 8 Clock fixed() method

Java 8ProgrammingObject Oriented Programming

The fixed instant on the clock can be obtained using the method fixed() in the Clock Class in Java. This method requires two parameters i.e. the fixed instant and the time zone. Also, it returns the fixed instant on the clock. The fixed() method is normally used for testing purposes.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      Instant i = Instant.now();
      ZoneId zId = ZoneId.of("Australia/Melbourne");
      Clock c = Clock.fixed(i, zId);
      System.out.println(c.toString());
   }
}

Output

FixedClock[2019-02-07T09:19:36.449Z,Australia/Melbourne]

Now let us understand the above program.

The fixed instant on the clock is obtained using the method fixed() and then this is displayed. A code snippet that demonstrates this is as follows −

Instant i = Instant.now();
ZoneId zId = ZoneId.of("Australia/Melbourne");
Clock c = Clock.fixed(i, zId);
System.out.println(c.toString());
raja
Published on 13-Mar-2019 15:47:40
Advertisements