且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

告别jodatime!拥抱Java8日期时间类LocalDate、LocalDateTime详解(下)

更新时间:2022-05-15 13:28:19


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很好地建模了该领域,并提供了用于对各种开发人员用例进行建模的大量类。