为什么 java.time.Clock
有时区信息?在调用 instant()
方法时,您只能从 Clock 获得 Instant
- 这是没有时区信息的时间。
是让时钟中的区域可用的唯一目的,例如像这样创建一个 ZonedDateTime?
ZonedDateTime.ofInstant(clock().instant(), clock().getZone())
那么在 Clock 类中使用 zonedDateTime()
方法是否有意义?
最佳答案
备用时钟行为
引用Clock
文档(强调我的):
Use of a Clock is optional. All key date-time classes also have a now() factory method that uses the system clock in the default time zone. The primary purpose of this abstraction is to allow alternate clocks to be plugged in as and when required. Applications use an object to obtain the current time rather than a static method. This can simplify testing.
例如,Clock.fixed( Instant fixedInstant, ZoneId zone )
始终将当前时刻报告为特定时刻,即固定(不变)的时间点。
关于java - 为什么 java.time.Clock 有时区信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53517885/