java 8 ZonedDateTime和OffsetDateTime有什么区别? [英] What's the difference between java 8 ZonedDateTime and OffsetDateTime?

查看:929
本文介绍了java 8 ZonedDateTime和OffsetDateTime有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了文档,但是当我应该使用其中一个时我仍然无法获得:





根据文件 OffsetDateTime 应该在将日期写入数据库时​​使用,但我不明白为什么。

解决方案

javadocs这样说:


OffsetDateTime ZonedDateTime Instant 都会在时间线上存储一个纳秒级精度。 Instant 是最简单的,只需代表instant。 OffsetDateTime 添加到UTC / Greenwich的偏移瞬间,这允许获得本地日期时间。 ZonedDateTime 添加完整的时区规则。


来源: https://docs.oracle.com/javase/8/docs/api/java/time /OffsetDateTime.html



因此 OffsetDateTime 之间的区别ZonedDateTime 是后者包括涵盖夏令时调整的规则。


在写日期时应使用OffsetDateTime数据库,但我不明白为什么。


一个原因是具有本地时间偏移的日期总是代表相同的时刻,因此具有稳定的订购。相比之下,面对对各个时区的规则的调整,具有全时区信息的日期的含义是不稳定的。 (这些确实发生了......)



如果(例如)在日期的字段上创建数据库索引,则其含义/排序不稳定的日期会有问题。 / p>

I've read the documentation, but I still can't get when I should use one or the other:

According to documentation OffsetDateTime should be used when writing date to database, but I don't get why.

解决方案

The javadocs say this:

"OffsetDateTime, ZonedDateTime and Instant all store an instant on the time-line to nanosecond precision. Instant is the simplest, simply representing the instant. OffsetDateTime adds to the instant the offset from UTC/Greenwich, which allows the local date-time to be obtained. ZonedDateTime adds full time-zone rules."

Source: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html

Thus the difference between OffsetDateTime and ZonedDateTime is that the latter includes the rules that cover daylight saving time adjustments.

OffsetDateTime should be used when writing date to database, but I don't get why.

One reason is that dates with local time offsets always represent the same instants in time, and therefore have a stable ordering. By contrast, the meaning of dates with full timezone information is unstable in the face of adjustments to the rules for the respective timezones. (And these do happen...)

Dates whose meaning / ordering is unstable are problematic if (for example) you create a database index on a field the date.

这篇关于java 8 ZonedDateTime和OffsetDateTime有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆