首頁  >  文章  >  Java  >  java時間日期的API有哪些

java時間日期的API有哪些

王林
王林轉載
2023-05-01 19:55:041127瀏覽

1、Clock提供了存取當前時間和日期的功能。 Clock對目前時區敏感,可以用來取代System.currenttimeMillis()取得當前毫秒時間。

Clock clock = Clock.systemDefaultZone();
long millis = clock.millis();
 
Instant instant = clock.instant();
Date legacyDate = Date.from(instant);   // legacy java.util.Date

2、本機時間類別表示沒有指定時區的時間。

LocalTime now1 = LocalTime.now(zone1);
LocalTime now2 = LocalTime.now(zone2);
 
System.out.println(now1.isBefore(now2));  // false
 
long hoursBetween = ChronoUnit.HOURS.between(now1, now2);
long minutesBetween = ChronoUnit.MINUTES.between(now1, now2);
 
System.out.println(hoursBetween);       // -3
System.out.println(minutesBetween);     // -239

3、時區類別可以用ZoneId表示。 時區類別的物件可以透過靜態工廠方法輕鬆取得。

System.out.println(ZoneId.getAvailableZoneIds());
// prints all available timezone ids
 
ZoneId zone1 = ZoneId.of("Europe/Berlin");
ZoneId zone2 = ZoneId.of("Brazil/East");
System.out.println(zone1.getRules());
System.out.println(zone2.getRules());
 
// ZoneRules[currentStandardOffset=+01:00]
// ZoneRules[currentStandardOffset=-03:00]

以上是java時間日期的API有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除