java.Clock 中的混淆,systemDefaultZone() 返回 UTC 时间
Confusion in java.Clock, systemDefaultZone() returning UTC time
我想了解为什么以下 java.time.Clock
返回的是 UTC 时间而不是本地时区 (EST)。
C:\Users\Felipe>scala
Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_65).
Type in expressions for evaluation. Or try :help.
scala> import java.time._
import java.time._
scala> ZoneId.systemDefault()
res0: java.time.ZoneId = America/New_York
scala> val clock = Clock.systemDefaultZone()
clock: java.time.Clock = SystemClock[America/New_York]
scala> clock.instant
res1: java.time.Instant = 2017-07-06T16:20:04.990Z
我运行上面的当前时间是12:20pm
(即显示的UTC时间前4小时)
Instant.toString()
method uses DateTimeFormatter.ISO_INSTANT
formatter, which in turn parses and formats the Instant
in UTC.
由于 2017-07-06T16:20:04.990Z
与纽约的 2017-07-06T12:20:04.990
相同,您得到的结果是正确的。
如果你想把Instant
转换成你的时区,你可以这样做:
clock.instant().atZone(ZoneId.systemDefault())
或者你可以更具体(因为系统的默认时区可以更改,即使在运行时):
clock.instant().atZone(ZoneId.of("America/New_York"))
这将导致 ZonedDateTime
:
2017-07-06T12:48:22.890-04:00[America/New_York]
如果需要,您也可以将其转换为 LocalDateTime
:
clock.instant().atZone(ZoneId.of("America/New_York")).toLocalDateTime()
结果将是 LocalDateTime
:
2017-07-06T12:49:47.688
PS: as (我忘记说了), Instant
class 只代表一个点在时间上(自 1970-01-01T00:00Z
以来的纳秒数)并且没有时区信息,因此它的任何表示(包括 toString()
方法)都将采用 UTC。要获取对应于 Instant
的本地日期或时间,您必须提供时区,如上所示。
Instant
没有任何时区信息。它只是自纪元以来 seconds/nanoseconds 的数量。 LocalDateTime
表示本地时区的时间,可以通过clock获取:
LocalDateTime.now(clock)
您还可以使用以下方法将 Instant
转换为 ZonedDateTime
(表示时间和时区):
clock.instant().atZone(ZoneId.systemDefault())
我想了解为什么以下 java.time.Clock
返回的是 UTC 时间而不是本地时区 (EST)。
C:\Users\Felipe>scala
Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_65).
Type in expressions for evaluation. Or try :help.
scala> import java.time._
import java.time._
scala> ZoneId.systemDefault()
res0: java.time.ZoneId = America/New_York
scala> val clock = Clock.systemDefaultZone()
clock: java.time.Clock = SystemClock[America/New_York]
scala> clock.instant
res1: java.time.Instant = 2017-07-06T16:20:04.990Z
我运行上面的当前时间是12:20pm
(即显示的UTC时间前4小时)
Instant.toString()
method uses DateTimeFormatter.ISO_INSTANT
formatter, which in turn parses and formats the Instant
in UTC.
由于 2017-07-06T16:20:04.990Z
与纽约的 2017-07-06T12:20:04.990
相同,您得到的结果是正确的。
如果你想把Instant
转换成你的时区,你可以这样做:
clock.instant().atZone(ZoneId.systemDefault())
或者你可以更具体(因为系统的默认时区可以更改,即使在运行时):
clock.instant().atZone(ZoneId.of("America/New_York"))
这将导致 ZonedDateTime
:
2017-07-06T12:48:22.890-04:00[America/New_York]
如果需要,您也可以将其转换为 LocalDateTime
:
clock.instant().atZone(ZoneId.of("America/New_York")).toLocalDateTime()
结果将是 LocalDateTime
:
2017-07-06T12:49:47.688
PS: as Instant
class 只代表一个点在时间上(自 1970-01-01T00:00Z
以来的纳秒数)并且没有时区信息,因此它的任何表示(包括 toString()
方法)都将采用 UTC。要获取对应于 Instant
的本地日期或时间,您必须提供时区,如上所示。
Instant
没有任何时区信息。它只是自纪元以来 seconds/nanoseconds 的数量。 LocalDateTime
表示本地时区的时间,可以通过clock获取:
LocalDateTime.now(clock)
您还可以使用以下方法将 Instant
转换为 ZonedDateTime
(表示时间和时区):
clock.instant().atZone(ZoneId.systemDefault())