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

简介: 告别jodatime!拥抱Java8日期时间类LocalDate、LocalDateTime详解


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

目录
相关文章
|
5天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
33 3
|
1天前
|
人工智能 安全 Java
Java8 - LocalDateTime时间日期类使用详解
Java8 - LocalDateTime时间日期类使用详解
|
2天前
|
安全 Java 程序员
|
3天前
|
Java
Java Class类
Java Class类
8 0
|
3天前
|
安全 Java API
Java日期与时间
Java日期与时间
25 1
|
9天前
|
Java 编译器
Java Character 类
4月更文挑战第13天
|
10天前
|
存储 Java
Java基础教程(7)-Java中的面向对象和类
【4月更文挑战第7天】Java是面向对象编程(OOP)语言,强调将事务抽象成对象。面向对象与面向过程的区别在于,前者通过对象间的交互解决问题,后者按步骤顺序执行。类是对象的模板,对象是类的实例。创建类使用`class`关键字,对象通过`new`运算符动态分配内存。方法包括构造函数和一般方法,构造函数用于对象初始化,一般方法处理逻辑。方法可以有0个或多个参数,可变参数用`类型...`定义。`this`关键字用于访问当前对象的属性。
|
安全 Oracle Java
告别jodatime!拥抱Java8日期时间类LocalDate、LocalDateTime详解(上)
告别jodatime!拥抱Java8日期时间类LocalDate、LocalDateTime详解
194 0
告别jodatime!拥抱Java8日期时间类LocalDate、LocalDateTime详解(上)
|
3天前
|
安全 Java 调度
Java线程:深入理解与实战应用
Java线程:深入理解与实战应用
21 0
|
1天前
|
消息中间件 缓存 NoSQL
Java多线程实战-CompletableFuture异步编程优化查询接口响应速度
Java多线程实战-CompletableFuture异步编程优化查询接口响应速度