Java时钟类是日期时间API Java的一部分。时间时钟,爪哇的。Java日期时间API是从Java版本8添加的。
null
offset()方法是Clock类的一个静态方法,它返回一个时钟,其瞬时值等于作为参数传递的时钟的瞬时值和 特定偏移持续时间 。如果添加的持续时间为正,则返回的时钟表示从基准时钟开始的指定持续时间之后的时钟瞬间。如果添加的持续时间为负数,则返回的时钟早于基准时钟的日期和时间。持续时间为零对基准时钟不起作用,并返回基准时钟。
如果基本时钟是不可变的、线程安全的和可序列化的,那么返回的时钟也是不可变的、线程安全的和可序列化的。
语法:
public static Clock offset(Clock baseClock, Duration offsetDuration)
参数: 此方法接受两个强制参数:
- 基准时钟 –增加持续时间的时钟。它不能是空值。
- 抵消期 –与基准时钟一起添加的持续时间。它也不能是空值。
返回值: 此方法返回一个时钟,其瞬时值等于作为参数传递的时钟的瞬时值和特定偏移持续时间之和。
下面的程序演示了java的偏移(时钟基时钟、持续时间偏移持续时间)方法。时间时钟等级:
项目1: 当偏移量作为小时数传递时。
// Java program to demonstrate offset() // method of Clock class import java.time.*; // create class public class offsetMethodDemo { // Main method public static void main(String[] args) { // base Clock with default zone Clock realClock = Clock.systemDefaultZone(); // print current time System.out.println( "Real clock instant is " + realClock.instant()); // Creating another clock with offset 0 Clock clock = Clock.offset(realClock, Duration.ZERO); // print new clock System.out.println( "New clock instant" + " with Duration = 0 is " + clock.instant()); // Creating the clock with 24 hours positive offset clock = Clock.offset(realClock, Duration.ofHours( 24 )); // print new clock System.out.println( "New clock instant" + " with Duration = 24hours is " + clock.instant()); // Creating the clock with 24 hours negative offset clock = Clock.offset(realClock, Duration.ofHours(- 24 )); // print new clock System.out.println( "New clock instant" + " with Duration = -24hours is " + clock.instant()); } } |
输出:
Real clock instant is 2018-08-21T09:43:13.519Z New clock instant with Duration = 0 is 2018-08-21T09:43:13.785Z New clock instant with Duration = 24hours is 2018-08-22T09:43:13.785Z New clock instant with Duration = -24hours is 2018-08-20T09:43:13.785Z
项目2: 当偏移量以秒和分的形式传递时。
// Java program to demonstrate offset() // method of Clock class import java.time.*; // create class public class offsetMethodDemo { // Main method public static void main(String[] args) { // create a Zone Id for Europe/Paris ZoneId zoneId = ZoneId.of( "Europe/Paris" ); // base Clock with default zone Clock realClock = Clock.system(zoneId); // print current time System.out.println( "Real clock instant is " + realClock.instant()); // Creating the clock with 50 seconds positive offset Clock clock = Clock.offset(realClock, Duration.ofSeconds( 50 )); // print new clock System.out.println( "Time after 50 second later" + " than real Clock is " + clock.instant()); // Creating the clock with 30 minutes positive offset clock = Clock.offset(realClock, Duration.ofMinutes( 30 )); // print new clock System.out.println( "Time after 30 minutes later" + " than real Clock is " + clock.instant()); } } |
输出:
Real clock instant is 2018-08-21T09:43:18.921Z Time after 50 second later than real Clock is 2018-08-21T09:44:08.969Z Time after 30 minutes later than real Clock is 2018-08-21T10:13:18.969Z
参考: https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#offset-爪哇。时间时钟java。时间持续时间-
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END