java.time.Clock.withZone(ZoneId zone)方法是Clock類的一種方法,該方法返回應用了該方法的時鍾對象的時鍾副本,並帶有不同的時區。如果有時鍾,並且需要更改時鍾區域,但沒有其他屬性,則使用withZone()方法。此方法將zone作為參數,這是需要更改的時區。它返回帶區域的時鍾,與參數中傳遞的區域相同。
用法:
public abstract Clock withZone(ZoneId zone)
參數:此方法采用ZoneId類型的強製性參數區域,在其中需要更改時區。
返回:此方法返回應用了此方法的時鍾對象的時鍾副本,並以不同的時區作為參數傳遞。
例:
Code: //Clock with default zone Clock clock1=Clock.systemUTC(); ZoneId zone = ZoneId.of("Asia/Calcutta"); Clock clock2 = clock1.withZone(zone); System.out.println(clock2.toString()); Output:: SystemClock[Asia/Calcutta] Explanation:: when withZone() is called for Clock object clock1 with zoneId "Asia/Calcutta", then the withZone() method will return a Clock whose Zone is "Asia/Calcutta".
下麵的程序演示了java.time.Clock類的withZone()方法:
程序1:在withZone()的幫助下,創建具有與“第一時鍾”相似的屬性但zoneId等於“Asia/Calcutta”的時鍾。
// Java program to demonstrate
// withZone() method of Clock class
import java.time.*;
// create class
public class withZoneMethodDemo {
// Main method
public static void main(String[] args)
{
// create a base Clock with systemUTC() method
Clock baseclock = Clock.systemUTC();
// get ZonedDateTime object from baseclock
// clock instant to get date time
ZonedDateTime baseTime = baseclock
.instant()
.atZone(baseclock.getZone());
// print details of ZonedDateTime
System.out.println("ZonedDateTime of baseclock "
+ baseTime.toString());
// create ZoneId object with Zone Asia/Calcutta
ZoneId zone = ZoneId.of("Asia/Calcutta");
// apply withZone() to change zone from utc to Asia/Calcutta
Clock clockWithOtherZone = baseclock.withZone(zone);
// get ZonedDateTime object from clockWithOtherZone
// clock instant to get date time
ZonedDateTime calcuttaTime = clockWithOtherZone
.instant()
.atZone(clockWithOtherZone.getZone());
// print details of ZonedDateTime
System.out.println("ZonedDateTime of clockWithOtherZone "
+ calcuttaTime.toString());
}
}
輸出:
ZonedDateTime of baseclock 2018-08-24T08:09:17.354Z ZonedDateTime of clockWithOtherZone 2018-08-24T13:39:17.539+05:30[Asia/Calcutta]
程序2:使用getZone()為withZone()創建的時鍾打印zoneId。
// Java program to demonstrate
// withZone() method of Clock class
import java.time.*;
// create class
public class withZoneMethodDemo {
// Main method
public static void main(String[] args)
{
// create a base Clock with systemDefaultZone() method
Clock baseclock = Clock.systemDefaultZone();
// print details of ZonedDateTime
System.out.println("baseclock Zone:"
+ baseclock.getZone());
// create ZoneId object with Zone Asia/Calcutta
ZoneId zone = ZoneId.of("Asia/Calcutta");
// apply withZone() to change zone
// of baseclock to Asia/Calcutta
Clock clockWithOtherZone = baseclock.withZone(zone);
// print details of ZonedDateTime
System.out.println("clockWithOtherZone Zone:"
+ clockWithOtherZone.getZone());
}
}
輸出:
baseclock Zone:Etc/UTC clockWithOtherZone Zone:Asia/Calcutta
參考:
https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#withZone-java.time.ZoneId-
相關用法
- Java Clock tickMinutes()用法及代碼示例
- Java Clock systemUTC()用法及代碼示例
- Java 8 Clock instant()用法及代碼示例
- Java 8 Clock hashCode()用法及代碼示例
- Java Clock tick()用法及代碼示例
- Java 8 Clock fixed()用法及代碼示例
- Java 8 Clock offset()用法及代碼示例
- Java Clock systemDefaultZone()用法及代碼示例
- Java Clock system()用法及代碼示例
- Java 8 Clock getZone()用法及代碼示例
- Java 8 Clock millis()用法及代碼示例
- Java Clock tickSeconds()用法及代碼示例
- Java 8 Clock equals()用法及代碼示例
- Java HijrahChronology dateNow(Clock)用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Java Clock withZone() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。