天天看點

告别jodatime!擁抱Java8日期時間類LocalDate、LocalDateTime詳解(下)7 時區類8 時間段(period)9 持續時間(Duration)10 年表11 其餘的API12 總結

7 時區類

ZonedDateTime

是具有完全限定時區的日期和時間。這樣可以解決任何時間點的偏移。

最佳實踐:若要表示日期和時間而不依賴特定伺服器的上下文,則應使用

ZonedDateTime

ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Paris]");      

OffsetDateTime

是具有已解決偏移量的日期和時間。這對于将資料序列化到資料庫中很有用,如果伺服器在不同時區,則還應該用作記錄時間戳的序列化格式。

OffsetTime

是具有确定的偏移量的時間,如下:

OffsetTime time = OffsetTime.now();
// changes offset, while keeping the same point on the timeline
OffsetTime sameTimeDifferentOffset = time.withOffsetSameInstant(
    offset);
// changes the offset, and updates the point on the timeline
OffsetTime changeTimeWithNewOffset = time.withOffsetSameLocal(
    offset);
// Can also create new object with altered fields as before
changeTimeWithNewOffset
 .withHour(3)
 .plusSeconds(2);
OffsetTime time = OffsetTime.now();
// changes offset, while keeping the same point on the timeline
OffsetTime sameTimeDifferentOffset = time.withOffsetSameInstant(
    offset);
// changes the offset, and updates the point on the timeline
OffsetTime changeTimeWithNewOffset = time.withOffsetSameLocal(
    offset);
// Can also create new object with altered fields as before
changeTimeWithNewOffset
 .withHour(3)
 .plusSeconds(2);      

Java中已有一個時區類,java.util.TimeZone但Java SE 8并沒有使用它,因為所有JSR 310類都是不可變的并且時區是可變的。

8 時間段(period)

Period代表諸如“ 3個月零一天”的值,它是時間線上的距離。這與到目前為止我們讨論過的其他類形成了鮮明的對比,它們是時間軸上的重點。

// 3 年, 2 月, 1 天
Period period = Period.of(3, 2, 1);

// 使用 period 修改日期值
LocalDate newDate = oldDate.plus(period);
ZonedDateTime newDateTime = oldDateTime.minus(period);
// Components of a Period are represented by ChronoUnit values
assertEquals(1, period.get(ChronoUnit.DAYS)); 
// 3 years, 2 months, 1 day
Period period = Period.of(3, 2, 1);

// You can modify the values of dates using periods
LocalDate newDate = oldDate.plus(period);
ZonedDateTime newDateTime = oldDateTime.minus(period);
// Components of a Period are represented by ChronoUnit values
assertEquals(1, period.get(ChronoUnit.DAYS));       

9 持續時間(Duration)

Duration是時間線上按時間度量的距離,它實作了與相似的目的Period,但精度不同:

// 3 s 和 5 ns 的 Duration 
Duration duration = Duration.ofSeconds(3, 5);
Duration oneDay = Duration.between(today, yesterday);
// A duration of 3 seconds and 5 nanoseconds
Duration duration = Duration.ofSeconds(3, 5);
Duration oneDay = Duration.between(today, yesterday);      

可以對Duration執行個體執行正常的加,減和“ with”運算,還可以使用修改日期或時間的值Duration。

10 年表

為了滿足使用非ISO月曆系統的開發人員的需求,Java SE 8引入了Chronology,代表月曆系統,并充當月曆系統中時間點的工廠。也有一些接口對應于核心時間點類,但通過

Chronology:
ChronoLocalDate
ChronoLocalDateTime
ChronoZonedDateTime
Chronology:
ChronoLocalDate
ChronoLocalDateTime
ChronoZonedDateTime      

這些類僅适用于正在開發高度國際化的應用程式且需要考慮本地月曆系統的開發人員,沒有這些要求的開發人員不應使用它們。有些月曆系統甚至沒有一個月或一周的概念,是以需要通過非常通用的字段API進行計算。

11 其餘的API

Java SE 8還具有一些其他常見用例的類。有一個MonthDay類,其中包含一對Month和Day,對于表示生日非常有用。該YearMonth類涵蓋了信用卡開始日期和到期日期的用例以及人們沒有指定日期的場景。

Java SE 8中的JDBC将支援這些新類型,但不會更改公共JDBC API。現有的泛型setObject和getObject方法就足夠了。

這些類型可以映射到特定于供應商的資料庫類型或ANSI SQL類型。

12 總結

Java SE 8在java.time中附帶一個新的日期和時間API,為開發人員提供了大大改善的安全性和功能。新的API很好地模組化了該領域,并提供了用于對各種開發人員用例進行模組化的大量類。