Example usage for java.time Clock offset

List of usage examples for java.time Clock offset

Introduction

In this page you can find the example usage for java.time Clock offset.

Prototype

public static Clock offset(Clock baseClock, Duration offsetDuration) 

Source Link

Document

Obtains a clock that returns instants from the specified clock with the specified duration added

This clock wraps another clock, returning instants that are later by the specified duration.

Usage

From source file:Main.java

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());
}

From source file:Main.java

public static void main(String[] args) {
    Clock utcClock = Clock.systemUTC();
    Clock defaultClock = Clock.systemDefaultZone();
    Clock offsetClock = Clock.offset(Clock.systemUTC(), Duration.ofHours(-5));

    ZoneId denverTimeZone = ZoneId.of("America/Denver");
    ZoneId newYorkTimeZone = ZoneId.of("America/New_York");
    ZoneId chicagoTimeZone = ZoneId.of("America/Chicago");
    ZoneId losAngelesTimeZone = ZoneId.of("America/Los_Angeles");

    Instant instant = Instant.now(defaultClock);
    Instant instant2 = Instant.now(utcClock);
    Instant instant3 = Instant.now(offsetClock);

    System.out.println(instant);// w  ww .  jav  a  2  s  . c  o m
    System.out.println(instant2);
    System.out.println(instant3.plus(Duration.ofSeconds(90)));
    System.out.println(instant3.atZone(newYorkTimeZone));
    System.out.println(instant3.atZone(chicagoTimeZone));
    System.out.println(instant3.atZone(denverTimeZone));
    System.out.println(instant3.atZone(losAngelesTimeZone));
}