Java 8:Instant和LocalDateTime有什么区别? [英] Java 8: What's the difference between Instant and LocalDateTime?

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

问题描述

我知道:




  • 即时是一个技术时间戳表示(纳秒),用于计算

  • LocalDateTime 是日期/时钟表示,包括人类的时区。



仍然最终,IMO可以被视为大多数应用程序用例的类型。例如:目前我正在运行一个批处理工作,我需要根据日期计算一个下一个运行,而我正在努力寻找这两种类型之间的优缺点(除了Instant的nanoSec精度优势和时区部分的LocalDateTime)。



您可以列出一些应用示例,其中只能使用Instant或LocalDateTime吗?



>


LocalDateTime是包含人类时区的日期/时钟表示。


您的陈述不正确: A LocalDateTime 具有 no 时区。没有时区是该类的整个点。



引用该类'doc:


此类不存储或表示时区。相反,它是对日子的描述,如用于生日,结合当地时间在挂钟上看到的。它不能代表一个即时的时间线,没有额外的信息,如偏移或时区。


所以本地... 表示未分区。



即时



a href =http://docs.oracle.com/javase/8/docs/api/java/time/Instant.html =noreferrer> 即时 UTC 的时间线上的一段时间, nanoseconds 自1970年UTC的第一时刻的时代(基本上,参见class doc for nitty-gritty细节)。由于您的大多数业务逻辑,数据存储和数据交换均应采用UTC,因此这是一个经常使用的方便类。



ZoneId



A ZoneId 是一个时区



时区是距离UTC多少小时和分钟的偏移。 巴黎之前的新的一天早于Montréal。所以我们需要移动时钟的手,以更好地反映中午(当Sun直接开销时)一个给定的地区。距离欧洲的UTC线更远,偏移量越大。



另外,时区是一组处理由当地社区实施的调整和异常的规则或区域。最常见的异常是被称为夏令时(DST)的全受欢迎的疯狂。



时区具有过去规则,现有规则和规则,以及在不久的将来确认的规则。



这些规则的变化比您预期的更频繁。请务必保留日期库的规则,通常是'tz'数据库的副本,最新。在Java 8中,保持最新的比以往更容易,Oracle发布了一个时区更新工具



使用正确的时区名称。这些名称采取大陆的形式加上SLASH加上城市或地区。避免使用3-4字母代码,例如 EST IST 。它们既不是标准化的,也不是唯一的。他们进一步混淆了DST的混乱。


时区=偏移+调整规则


有时我们只有一个没有规则的偏移量。 Java提供了 ZoneOffset 为此目的, ZoneId 的子类。注意那里定义的常用常数, ZoneOffset.UTC



ZonedDateTime



a href =http://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html =noreferrer> ZonedDateTime 类是具有指定ZoneId的Instant。


ZonedDateTime = Instant + ZoneId


< blockquote>

几乎所有的后端,数据库,业务逻辑,数据持久性,数据交换都应该在UTC。但是,要向用户演示,您需要将其调整为用户期望的时区。这是 ZonedDateTime 类和 formatter类用于生成这些日期时间值的String表示。



LocalDateTime,LocalDate ,LocalTime



本地日期时间类, LocalDateTime LocalDate LocalTime 是一种不同的小动物。没有任何一个地方或时区。他们并没有束缚于时间表。 他们没有真正的意义,直到你将它们应用到一个地点在时间线上找到一个点。



例如,圣诞节从午夜开始于2015年12月25日是LocalDateTime。午夜在巴黎的不同时刻罢工,而不是在蒙特利尔,而西雅图奥克兰



另一个例子是Acme公司有一项政策,午餐时间从12:30 PM开始在其全球各个工厂是一个LocalTime。要具有真正的意义,您需要将其应用于时间轴,以便在斯图加特工厂或12:30在拉巴特工厂或12:30在 Sydney 工厂。



所以对于商业应用,本地类型并不经常被使用,因为它们仅代表可能的日期或时间的一般想法,而不是时间线上的特定时刻。商业应用程序倾向于关注发票到达的确切时刻,运输的产品,雇用的员工,或者出租车离开车库。所以商业应用程序开发人员几乎完全使用 Instant ZonedDateTime


I know that:

  • Instant is rather a "technical" timestamp representation (nanoseconds) for computing.
  • LocalDateTime is rather date/clock representation including time-zones for humans.

Still in the end IMO both can be taken as type for most application use-cases. As example: Currently I am running a batch job where I need to calculate a next run based on dates and I am struggling to find a pros/cons between these two types (apart from the nanosecond precision advantage of Instant and the time-zone part of LocalDateTime).

Can you name some application examples, where only Instant or LocalDateTime should be used?

Edit: Beware misread documentations for LocalDateTime regarding precision and time-zone

解决方案

Incorrect Presumption

LocalDateTime is rather date/clock representation including time-zones for humans.

Your statement is incorrect: A LocalDateTime has no time zone. Having no time zone is the entire point of that class.

To quote that class’ doc:

This class does not store or represent a time-zone. Instead, it is a description of the date, as used for birthdays, combined with the local time as seen on a wall clock. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.

So Local… means "not zoned".

Instant

An Instant is a moment on the timeline in UTC, a count of nanoseconds since the epoch of the first moment of 1970 UTC (basically, see class doc for nitty-gritty details). Since most of your business logic, data storage, and data exchange should be in UTC, this is a handy class to be used often.

ZoneId

A ZoneId is a time zone.

A time zone is an offset of so many hours and minutes away from UTC. A new day dawns earlier in Paris than in Montréal, for example. So we need to move the clock’s hands to better reflect noon (when the Sun is directly overhead) for a given region. The further away from the UTC line in Europe the larger the offset.

Plus, a time zone is a set of rules for handling adjustments and anomalies as practiced by a local community or region. The most common anomaly is the all-too-popular lunacy known as Daylight Saving Time (DST).

A time zone has the history of past rules, present rules, and rules confirmed for the near future.

These rules change more often than you might expect. Be sure to keep your date-time library's rules, usually a copy of the 'tz' database, up to date. Keeping up-to-date is easier than ever now in Java 8 with Oracle releasing a Timezone Updater Tool.

Use proper time zone names. These names take the form of continent plus a SLASH plus a city or region. Avoid the 3-4 letter codes such as EST or IST. They are neither standardized nor unique. They further confuse the messiness of DST.

Time Zone = Offset + Rules of Adjustments

Sometimes we have only an offset without the rules. Java provides the ZoneOffset for this purpose, a subclass of ZoneId. Note the handy constant defined there, ZoneOffset.UTC.

ZonedDateTime

The ZonedDateTime class is an Instant with an assigned ZoneId.

ZonedDateTime = Instant + ZoneId

Nearly all of your backend, database, business logic, data persistence, data exchange should all be in UTC. But for presentation to users you need to adjust into a time zone expected by the user. This is the purpose of the ZonedDateTime class and the formatter classes used to generate String representations of those date-time values.

LocalDateTime, LocalDate, LocalTime

The "local" date time classes, LocalDateTime, LocalDate, LocalTime, are a different kind of critter. The are not tied to any one locality or time zone. They are not tied to the timeline. They have no real meaning until you apply them to a locality to find a point on the timeline.

For example, "Christmas starts at midnight on the 25th of December 2015" is a LocalDateTime. Midnight strikes at different moments in Paris than in Montréal, and different again in Seattle and in Auckland.

Another example, "Acme Company has a policy that lunchtime starts at 12:30 PM at each of its factories worldwide" is a LocalTime. To have real meaning you need to apply it to the timeline to figure the moment of 12:30 at the Stuttgart factory or 12:30 at the Rabat factory or 12:30 at the Sydney factory.

So for business apps, the "Local" types are not often used as they represent just the general idea of a possible date or time not a specific moment on the timeline. Business apps tend to care about the exact moment an invoice arrived, a product shipped for transport, an employee was hired, or the taxi left the garage. So business app developers use Instant and ZonedDateTime almost exclusively.

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

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