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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。