Java Date Time - Clock fixed(Instant fixedInstant, ZoneId zone) example








Clock fixed(Instant fixedInstant, ZoneId zone) returns a clock that always returns the same instant.

Syntax

fixed has the following syntax.

public static Clock fixed(Instant fixedInstant,   ZoneId zone)

Example

The following example shows how to use fixed.

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
// w ww .j a  va 2s  .  c o  m
public class Main {
  public static void main(String[] args) {
    Instant instant = Instant.now();
    Clock newClock = Clock.fixed(instant,ZoneId.systemDefault());
    
    System.out.println(newClock.instant());
  }
}

The code above generates the following result.