java - 为什么 java.time.Clock 有时区信息?

标签 java timezone java-time jsr310

为什么 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/

相关文章:

java - 计算符合 GC 条件的对象数量

go - 获取 Go 中的有效时区列表

c# - .NET NodaTime 如何创建自定义时区?

clojure - 如何解构即时并从 java 时间库中提取年、月等

Java - Cipher 自定义提供程序

java - Spring 批处理 : How to configure a FlatItemFileReader to read a compressed ND-JSON file?

c# - 在 C# 中创建特定时区的 DateTime

具有固定毫秒数字的 java.time ISO 日期格式(在 Java 8 及更高版本中)

java - 检查给定的 Instant 是否符合定义的 Period

java - Kafka - 在运行时更改拓扑属性