英文:
What is the difference between Clock.systemUTC() and Clock.systemDefaultZone()?
问题
我正在查看Java中的Clock类,以便将其注入为依赖项,以实现更好的可测试性。但我不明白两种方法systemUTC
和systemDefaultZone
之间的区别。Instant.now()
似乎在内部使用systemUTC
时钟,而YearMonth.now()
使用systemDefaultZone
时钟。我已经阅读了Javadoc,但对我来说并不是很清楚。
我有一些使用Instant.now()
和YearMonth.now()
的代码,并想了解两个时钟之间的差异,以及在错误地使用每个时钟的情况下可能导致问题的一些示例。
英文:
I'm looking at the Clock class from java so that I can inject it as a dependency for better testability. But I don't understand the difference between the two methods systemUTC
and systemDefaultZone
. The Instant.now()
seems to be internally using systemUTC
clock whereas YearMonth.now()
uses the systemDefaultZone
clock. I already read the javadoc but it wasn't very clear to me.
I have code that use Instant.now()
and YearMonth.now()
and would like to understand the difference between the two clocks with some examples where using each the clocks wrongly can lead to a problem.
答案1
得分: 12
关于Clock.systemUTC
和Clock.systemDefaultZone
返回的两个时钟的instant
方法,它们将执行相同的操作。它们的文档都表示这些时钟是“基于最佳可用系统时钟”的。因此,使用这些时钟创建Instant
对象将始终给您相同的瞬间。
差异在于当您尝试创建日期或时间时。systemUTC
执行以下操作:
从瞬间到日期或时间的转换使用UTC时区。
而systemDefaultZone
执行以下操作:
使用默认时区进行日期和时间的转换。
因此,尽管如果询问它们当前的瞬间时,它们都将返回相同的瞬间,但它们不一定会返回相同的LocalDateTime
、LocalDate
或ZonedDateTime
。例如,假设我的系统时区是Asia/Shanghai
,这个时区全年都处于UTC+8的偏移量。而且现在在上海是2020-07-29早上5点。如果我使用LocalDate.now(Clock.systemUTC())
,它会告诉我是2020-07-28
,因为在UTC时区仍然是28号。但是,如果我使用LocalDate.now(Clock.systemDefaultZone())
,它会告诉我是2020-07-29
,因为这是上海的日期(这是系统时区)。
现在您应该明白为什么与日期/时间相关的类的无参数now
方法使用systemDefaultZone
。如果我在上海执行LocalTime.now()
并看到比实际时间早8小时的时间,那将会很奇怪!
英文:
In terms of the instant
method of the two clocks returned by Clock.systemUTC
and Clock.systemDefaultZone
, they will do the same thing. Both of their documentation says that the clocks are "based on the best available system clock". So creating Instant
s using the clocks will always give you the same instant.
The difference in behaviour comes when you try to create a date or time. systemUTC
does this:
> Conversion from instant to date or time uses the UTC time-zone.
Whereas systemDefaultZone
does this:
> converting to date and time using the default time-zone.
So although both will return the same instant if you ask them what the current instant is, they will not necessarily return the same LocalDateTime
, or LocalDate
, or ZonedDateTime
. For example, suppose my system timezone is Asia/Shanghai
, which is at an offset of UTC+8 all year round. And it is now 5 am on 2020-07-29 in Shanghai. If I do LocalDate.now(Clock.systemUTC())
, it will tell me 2020-07-28
, because it's still the 28th in the UTC timezone. If I do LocalDate.now(Clock.systemDefaultZone())
, however, it will tell me 2020-07-29
, because it is 2020-07-29 in Shanghai (which is the system timezone).
Now you should see why the parameterless now
of date/time related classes use systemDefaultZone
. It would be weird if I did LocalTime.now()
in Shanghai and saw a time that's 8 hours earlier!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论