Java
Clock.offset
获取一个时钟,从指定的时钟中返回添加了指定持续时间的实例。
如果持续时间为负数,则该时刻将比当前时刻早。
Clock.offset
可以用来获得一个时钟,它将给出过去或未来的瞬间。
如果我们传递零持续时间,我们将得到基础时钟。
在Java文档中找到Clock.offset的方法声明。
public static Clock offset(Clock baseClock, Duration offsetDuration)
其中,baseClock
是将被添加或减去持续时间的时钟。
offsetDuration
是添加或减去基本时钟的持续时间。
Clock.offset
将返回以基础时钟为基础的、添加或减去持续时间的时钟。
持续时间为秒
在这里,我们将使用Clock.offset
来获得过去和未来的时钟,持续时间由Duration.ofSeconds
给出。
DurationSeconds.java
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());
}
}
输出
2021-06-13T07:34:37.020Z
2021-06-13T07:34:47.030Z
2021-06-13T07:34:27.030Z
2021-06-13T07:34:37.030Z
在上面的例子中,我们得到未来10秒的时钟和过去10秒的时钟。
我们还可以看到,当我们通过零持续时间时,我们得到了原始时间的时钟。
持续时间为分钟
这里我们将使用Clock.offset
来获得过去和未来的时钟,其持续时间由Duration.ofMinutes
给出。
DurationMinutes.java
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());
}
}
输出
2021-06-13T07:41:11.839Z
2021-06-13T07:56:11.848Z
2021-06-13T07:26:11.848Z
在上面的例子中,我们得到未来 15 分钟的时钟 和过去 15 分钟的时钟。
持续时间为小时
这里我们将使用Clock.offset
来获得过去和未来的时钟,其持续时间由Duration.ofHours
给出。
DurationHours.java
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());
}
}
输出
2021-06-13T07:43:18.267Z
2021-06-13T12:43:18.276Z
2021-06-13T02:43:18.276Z
在上面的例子中,我们得到了未来5小时的时钟和过去5小时的时钟。
持续时间为天
这里我们将使用Clock.offset
来获得过去和未来的时钟,其持续时间由Duration.ofDays
给出。
DurationDays.java
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());
}
}
输出
2021-06-13T07:45:07.420Z
2021-06-21T07:45:07.429Z
2021-06-05T07:45:07.429Z
在上面的例子中,我们得到未来8天的时钟和过去8天的时钟。
参考文献
【1】Java Doc: Class Clock
【2】Java Clock 详解
【3】Java Clock offset()