Java 8的新Java日期时间API是否适用于DST? [英] Does Java 8's new Java Date Time API take care of DST?
问题描述
我正在考虑使用新的 java 8 Date Time API 。我google了一下,发现jodaTime是java的好选择,但仍然有兴趣了解这个新的API是如何工作的。
I am thinking of using the new java 8 Date Time API. I googled a bit and found jodaTime as good choice for java but still kind of interested to see how this new API works.
我正在将所有时间都存储在数据存储区中的UTC值,并将根据用户的时区将其转换为本地时区的具体值。我可以找到许多文章展示如何使用新的Java Date Time API。不过我不确定API是否会照顾DST的变化?
或者我们有什么更好的处理方式吗?
I am storing all time in UTC values in my datastore and will be converting them to Local Time Zone specific value based on user's timezone. I can find many articles showing how to use new Java Date Time API. However I am not sure if the API will take care of DST changes ? Or do we have any better way of handling Date ?
我正在学习新的Date API,所以想到听到你对处理DateTime和
I am just learning the new Date API , so thought of hearing your thoughts on handling the DateTime and displaying it on the basis of Users TimeZone.
推荐答案
这取决于你使用哪个类:
It depends on which class you use:
-
Instant
是全球时间线(UTC)上的瞬时点,与时区无关。 -
LocalDate
和LocalDateTime
没有时区的概念, c code当然可以给你正确的时间。 -
OffsetDateTime
有一个时区,但不支持夏令时。 -
ZonedDateTime
具有全时区支持。 >
Instant
is an instantaneous point on the global time-line (UTC), and is unrelated to time-zone.LocalDate
andLocalDateTime
have no concept of time-zone, but callingnow()
will of course give you your correct time.OffsetDateTime
has a time-zone, but doesn't support Daylight Savings Time.ZonedDateTime
has full time-zone support.
他们之间的转换通常需要一个时区,所以要回答你的问题:
Converting between them usually requires a time-zone, so to answer your question:
是的,Java 8日期/时间可以照顾DST,
Yes, Java 8 Date/Time can take care of DST, if you use it right.
这篇关于Java 8的新Java日期时间API是否适用于DST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!