Java Clock offset()

By Arvind Rai, May 17, 2019
Java Clock.offset obtains a clock that returns instants from the specified clock with the specified duration added. If the duration is negative, the instant will be earlier than the current instant. The Clock.offset can be used to get a clock that will give instant of past or future. If we pass zero duration, we will get underlying clock.
Find the method declaration of Clock.offset from Java doc.
public static Clock offset(Clock baseClock, Duration offsetDuration) 
Where baseClock is the clock to which duration will be added or subtracted. offsetDuration is the duration added or subtracted to base clock. Clock.offset will return the clock based on base clock with added or subtracted duration.

Duration in Seconds

Here we will use Clock.offset to get past and future clock with duration in seconds given by Duration.ofSeconds.
DurationSeconds.java
package com.concretepage;
import java.time.Clock;
import java.time.Duration;
public class DurationSeconds {
  public static void main(String[] args) {
	Clock baseClock = Clock.systemDefaultZone();
	System.out.println(baseClock.instant());
	
	//Obtained clock will be ahead by 10 seconds to the baseClock. 
	Clock clock = Clock.offset(baseClock, Duration.ofSeconds(10));
	System.out.println(clock.instant());

	//Obtained clock will be earlier by 10 seconds to the baseClock.
	clock = Clock.offset(baseClock, Duration.ofSeconds(-10));
	System.out.println(clock.instant());

	//Obtained clock will be same as baseClock
	clock = Clock.offset(baseClock, Duration.ZERO);
	System.out.println(clock.instant());
  }
} 
Output
2019-05-17T15:07:41.928023800Z
2019-05-17T15:07:51.959223900Z
2019-05-17T15:07:31.959223900Z
2019-05-17T15:07:41.959223900Z 
In the above example we get future clock by 10 seconds and a past clock by 10 seconds. We can also see that when we pass zero duration, we get the underlying clock.

Duration in Minutes

Here we will use Clock.offset to get past and future clock with duration in minutes given by Duration.ofMinutes.
DurationMinutes.java
package com.concretepage;
import java.time.Clock;
import java.time.Duration;
public class DurationMinutes {
  public static void main(String[] args) {
	Clock baseClock = Clock.systemDefaultZone();
	System.out.println(baseClock.instant());

	// Obtained clock will be ahead by 15 minutes to the baseClock
	Clock clock = Clock.offset(baseClock, Duration.ofMinutes(15));
	System.out.println(clock.instant());

	// Obtained clock will be earlier by 15 minutes to the baseClock
	clock = Clock.offset(baseClock, Duration.ofMinutes(-15));
	System.out.println(clock.instant());
  }
} 
Output
2019-05-17T15:08:33.465518400Z
2019-05-17T15:23:33.496718500Z
2019-05-17T14:53:33.496718500Z 
In the above example we get future clock by 15 minutes and a past clock by 15 minutes.

Duration in Hours

Here we will use Clock.offset to get past and future clock with duration in hours given by Duration.ofHours.
DurationHours.java
package com.concretepage;
import java.time.Clock;
import java.time.Duration;
public class DurationHours {
  public static void main(String[] args) {
	Clock baseClock = Clock.systemDefaultZone();
	System.out.println(baseClock.instant());

	// Obtained clock will be ahead by 5 hours to the baseClock
	Clock clock = Clock.offset(baseClock, Duration.ofHours(5));
	System.out.println(clock.instant());

	// Obtained clock will be earlier by 5 hours to the baseClock
	clock = Clock.offset(baseClock, Duration.ofHours(-5));
	System.out.println(clock.instant());
  }
} 
Output
2019-05-17T15:09:23.900407Z
2019-05-17T20:09:23.931607100Z
2019-05-17T10:09:23.931607100Z 
In the above example we get future clock by 5 hours and a past clock by 5 hours.

Duration in Days

Here we will use Clock.offset to get past and future clock with duration in days given by Duration.ofDays.
DurationDays.java
package com.concretepage;
import java.time.Clock;
import java.time.Duration;
public class DurationDays {
  public static void main(String[] args) {
	Clock baseClock = Clock.systemDefaultZone();
	System.out.println(baseClock.instant());

	// Obtained clock will be ahead by 8 days to the baseClock
	Clock clock = Clock.offset(baseClock, Duration.ofDays(8));
	System.out.println(clock.instant());

	// Obtained clock will be earlier by 8 days to the baseClock
	clock = Clock.offset(baseClock, Duration.ofDays(-8));
	System.out.println(clock.instant());
  }
} 
Output
2019-05-17T15:10:25.556116400Z
2019-05-25T15:10:25.587316500Z
2019-05-09T15:10:25.587316500Z 
In the above example we get future clock by 8 days and a past clock by 8 days.

References

Java Doc: Class Clock
Java Clock
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us