java.time.Clock.offset() Method Example



Description

The java.time.Clock.offset() method obtains a clock that returns instants from the specified clock with the specified duration added.

Declaration

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

public static Clock offset(Clock baseClock,Duration offsetDuration)

Parameters

  • baseClock − the base clock to add the duration to, not null.

  • offsetDuration − the duration to add, not null.

Return Value

a clock based on the base clock with the duration added, not null.

Example

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

package com.tutorialspoint;

import java.time.Clock;
import java.time.Duration;

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

      Clock clock = Clock.systemUTC();  
      Duration duration = Duration.ofHours(5);  
      Clock clock1 = Clock.offset(clock, duration);   
      System.out.println("Clock 1: " + clock.instant());
      System.out.println("Clock 2: " + clock1.instant());
   }
}

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

Clock 1: 2017-03-07T06:36:06.258Z
Clock 2: 2017-03-07T11:36:06.329Z
Advertisements