为什么 org.joda.time.LocalDate 是没有时区的日期?

Why is org.joda.time.LocalDate a date without a timezone?

我觉得 org.joda.time.LocalDate 这个名字有点令人困惑。文档说:

LocalDate is an immutable datetime class representing a date without a time zone.

相比之下 org.joda.time.DateTime 说:

A DateTime calculates its fields with respect to a time zone.

我总是把这两个混淆所以我希望有人能告诉我为什么这些名字应该有意义。


这是我的直觉:一个 local 日期或时间对象将代表一个时间点,但与 location 有关。因此它应该包含时区信息,因为时区还为您提供了某种 location 信息。无论如何,与没有那个时区的情况相比,您对某个人的位置了解更多。

日期时间,至少听起来应该只表示日期和时间。从这个意义上说,它只是自 00:00:00 协调世界时 (UTC),星期四,1970 年 1 月 1 日以来的 long 值。


那么为什么实际上是相反的呢?每当我与时间戳有关时,这个命名都会让我烦恼。有人可以解释一下吗?

更新:

有趣 link 发布者 user: https://sourceforge.net/p/threeten/mailman/message/29962542/

术语

您的直觉与 Joda-Time 和 java.time 使用的术语相反。

两个框架都有一个名为 LocalDate 的 class 来表示没有时间和时区的仅日期值。 “本地”的意思是“可以是任何地方,而不是任何特定的地方”。

“本地……”classes 只是一个粗略的时间概念。他们不在时间线上。在您应用时区或与 UTC 的偏移量以获取时间轴上的实际时刻之前,它们没有任何实际意义。当您看到“Local”时,您会想:“在我们应用时区之前,这个值没有任何意义”。

比如我们说今年圣诞节是2016-12-25。但是在任何给定时刻,世界各地的日期都不相同。例如,巴黎的新一天比蒙特利尔早。因此,要获取巴黎圣诞节开始的时刻,您必须应用 Europe/Paris 时区并获取当天的第一个时刻。第一时刻将在 Joda-Time 中由 DateTime 表示,在 java.time.

中由 ZonedDateTime(或 OffsetDateTime)表示

例如,在java.time中:

LocalDate xmas2016 = LocalDate.of( 2016 , 12 , 25 );
ZonedDateTime xmas2016FirstMomentMontreal = xmas2016.atStartOfDay( ZoneId.of( "America/Montreal" ) );

java.time

在 java.time 框架中,UTC 时间轴上的时刻由 Instant class.

表示
Instant now = Instant.now();

应用时区 ZoneId 以获得 ZonedDateTime

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );

如果您拥有的只是与 UTC 的偏移量而不是完整时区,则不要使用 OffsetDateTime 而不是 ZonedDateTime

你可以这么想:

Instant + ZoneId -> ZonedDateTime

Instant + ZoneOffset ->OffsetDateTime

此信息已在 Whosebug 上被提及数百次。请搜索以了解更多信息并查看许多示例代码。