Java Clock类是Java的Date Time API(java.time.Clock)的一部分。 Java日期时间API是从Java版本8添加的。
offset()方法是Clock类的静态方法,该方法返回一个时钟,该时钟的瞬时等于作为参数传递的时钟瞬时与特定偏移持续时间之和。如果添加的持续时间为正,则返回的时钟代表从基础时钟开始的指定持续时间后的时钟时刻。如果添加的持续时间为负,则返回的时钟早于基本时钟的日期和时间。持续时间为零不会对基本时钟产生任何作用,并返回基本时钟。
如果基本时钟是不可变的,线程安全的和可序列化的,则返回的时钟也是不可变的,线程安全的和可序列化的。
用法:
public static Clock offset(Clock baseClock, Duration offsetDuration)
参数:此方法接受两个强制性参数:
- baseclock–增加持续时间的时钟。不能为空值。
- offsetDuration–使用baseClock添加的持续时间。也不能为空值。
返回值:此方法返回的时钟瞬时等于作为参数传递的时钟瞬时与特定偏移持续时间之和。
以下示例程序旨在说明java.time.Clock类的offset(Clock baseClock,Duration offsetDuration)方法:
示例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
相关用法
- Java Clock systemUTC()用法及代码示例
- Java Clock systemDefaultZone()用法及代码示例
- Java Clock system()用法及代码示例
- Java Clock tickSeconds()用法及代码示例
- Java Clock tick()用法及代码示例
- Java 8 Clock getZone()用法及代码示例
- Java 8 Clock equals()用法及代码示例
- Java 8 Clock fixed()用法及代码示例
- Java 8 Clock millis()用法及代码示例
- Java 8 Clock instant()用法及代码示例
- Java 8 Clock hashCode()用法及代码示例
- Java Clock withZone()用法及代码示例
- Java Clock tickMinutes()用法及代码示例
- Java HijrahChronology dateNow(Clock)用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Java 8 Clock offset() method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。