java.time.Clock.fixed() Method Example



Description

The java.time.Clock.fixed(Instant fixedInstant, ZoneId zone) method obtains a clock that always returns the same instant.

Declaration

Following is the declaration for java.time.Clock.fixed() method.

public static Clock fixed(Instant fixedInstant, ZoneId zone)

Parameter

  • fixedInstant − the instant to use as the clock, not null.

  • zone − the time-zone to use to convert the instant to date-time, not null.

Return Value

a clock that always returns the same instant, not null.

Example

The following example shows the usage of java.time.Clock.fixed() method.

package com.tutorialspoint;

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;

public class ClockDemo {
   public static void main(String[] args) {

      Clock clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault());
      Clock clock1 = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault());

      System.out.println("Clock 1: " + clock.toString());
      System.out.println("Clock 2: " + clock1.toString());
   }
}

Let us compile and run the above program, this will produce the following result −

Clock 1: FixedClock[1970-01-01T01:00:00Z,Asia/Calcutta]
Clock 2: FixedClock[1970-01-01T01:00:00Z,Asia/Calcutta]
Advertisements