且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JSR 310 :: System.currentTimeMillis() 与 Instant.toEpochMilli() :: TimeZone

更新时间:2023-01-17 08:13:17

System.currentTimeMillis()Instant.toEpochMilli() 都返回自Unix 时代.尽管 Unix 纪元通常表示为UTC 时间 1970 年 1 月 1 日午夜",但这并不在"任何特定时区.但是瞬间只是时间的瞬间,无论您在哪个时区都是一样的 - 但它会反映不同的本地时间.

Both System.currentTimeMillis() and Instant.toEpochMilli() return the number of milliseconds since the Unix epoch. That isn't "in" any particular time zone, although the Unix epoch is normally expressed as "midnight on January 1st 1970, UTC". But an instant is just an instant in time, and is the same whichever time zone you're in - but it will reflect a different local time.

LocalDateTime.atZone(UTC) 的输出不同,因为您说的是获取本地日期和时间,并将其转换为即时就好像它在 UTC 时间zone" - 即使您在创建 LocalDateTime 时隐含地在 UTC+3 时区这样做......这就是它错误"的原因.

The output of LocalDateTime.atZone(UTC) differs because you're saying "Take the local date and time, and convert it to an instant as if it were in the UTC time zone" - even though when you created that LocalDateTime you did so implicitly in the UTC+3 time zone... that's why it's "wrong".

LocalDateTime.now() 采用系统默认时区中的本地日期和时间.因此,如果您的时区是 UTC+3,则当前时间是 2015-10-06T16:57:00Z,那么 LocalDateTime.now() 将返回 .2015-10-06T19:57:00.我们称之为 localNow...

LocalDateTime.now() takes the local date and time in the system default time zone. So if your time zone is UTC+3, the current instant in time is 2015-10-06T16:57:00Z, then LocalDateTime.now() will return .2015-10-06T19:57:00. Let's call that localNow...

所以 localNow.atZone(ZoneOffset.of("+3")) 将返回一个 ZonedDateTime 代表 2015-10-06T19:57:00+03 - in换句话说,相同的本地日期/时间,但知道"它比 UTC 早 3 小时......所以 toInstant() 将返回一个代表 2015-10 的 Instant-06T16:57:00Z.太好了 - 我们还有当前的日期/时间.

So localNow.atZone(ZoneOffset.of("+3")) will return a ZonedDateTime representing 2015-10-06T19:57:00+03 - in other words, the same local date/time, but "knowing" that it's 3 hours ahead of UTC... so toInstant() will return an Instant representing 2015-10-06T16:57:00Z. Great - we still have the current date/time.

localNow.atZone(ZoneOffset.UTC) 将返回代表 2015-10-06T19:57:00Z 的 ZonedDateTime - 换句话说,相同的本地日期/时间,但认为"它已经在 UTC...所以 toInstant() 将返回一个代表 2015-10-06T19:57:00Z 的 Instant .. 这是根本不是当前时间(三小时后).

But localNow.atZone(ZoneOffset.UTC) will return a ZonedDateTime representing 2015-10-06T19:57:00Z - in other words, the same local date/time, but "thinking" that it's already in UTC... so toInstant() will return an Instant representing 2015-10-06T19:57:00Z.. which isn't the current time at all (it's in three hours).