Java Date Time - Clock offset(Clock baseClock, Duration offsetDuration) example








Clock offset(Clock baseClock, Duration offsetDuration) gets a clock that returns instants from the specified clock with the specified duration added.

The duration can be negative.

Syntax

offset has the following syntax.

public static Clock offset(Clock baseClock,   Duration offsetDuration)

Example

The following example shows how to use offset.

import java.time.Clock;
import java.time.Duration;
//w  ww.ja va 2 s. c o  m
public class Main {
  public static void main(String[] args) {
    Clock clock = Clock.systemUTC();
    Duration duration = Duration.ofHours(3);
    Clock newClock = Clock.offset(clock,duration);
    
    System.out.println(newClock.instant());
  }
}

The code above generates the following result.