[英]why ZonedDateTime class does not implement TemporalAdjuster interface
I'm currently studying java.time API and I have noticed that majority of class (eg LocalDate
, OffsetDateTime
) in java.time implement TemporalAdjuster
interface, but ZonedDateTime
does not. 我目前正在研究java.time API,我注意到java.time中的大多数类(例如LocalDate
, OffsetDateTime
)实现了TemporalAdjuster
接口,但ZonedDateTime
却没有。 I was just wondering why this is the case? 我只是想知道为什么会这样? Why exclude ZonedDateTime
from implementing TemporalAdjuster
interface? 为什么要排除ZonedDateTime
实现TemporalAdjuster
接口?
A TemporalAdjuster
changes another temporal object via the TemporalAdjuster.adjustInto(Temporal)
method. TemporalAdjuster
通过TemporalAdjuster.adjustInto(Temporal)
方法更改另一个时态对象。 The Temporal
interface allows the individual fields to be altered via Temporal.with(TemporalField, long)
. Temporal
接口允许通过Temporal.with(TemporalField, long)
更改各个字段。
LocalDate
can implement TemporalAdjuster
because its state consists entirely of temporal fields (year, month, day-of-month). LocalDate
可以实现TemporalAdjuster
因为它的状态完全由时间字段(年,月,日)组成。 As such, the implementation in LocalDate.adjustInto(Temporal)
can call Temporal.with(TemporalField, long)
passing the year, month and day (it actually uses ChronoField.EPOCH_DAY
, which is a composite of year, month and day). 因此, LocalDate.adjustInto(Temporal)
的实现可以调用Temporal.with(TemporalField, long)
传递年,月和日(它实际上使用ChronoField.EPOCH_DAY
,它是年,月和日的组合)。
OffsetDateTime
can implement TemporalAdjuster
because its state also consists entirely of temporal fields (year, month, day-of-month, hour, minute, second, nanosecond and offset-seconds). OffsetDateTime
可以实现TemporalAdjuster
因为它的状态也完全由时间字段组成(年,月,日,小时,分钟,秒,纳秒和偏移秒)。 Thus, again the implementation in OffsetDateTime.adjustInto(Temporal)
can call Temporal.with(TemporalField, long)
passing the fields one-by-one. 因此, OffsetDateTime.adjustInto(Temporal)
的实现再次可以调用Temporal.with(TemporalField, long)
逐个传递字段。
ZonedDateTime
cannot implement TemporalAdjuster
because its state includes a ZoneId
, which is not a temporal field, thus cannot be passed to Temporal.with(TemporalField, long)
. ZonedDateTime
无法实现TemporalAdjuster
因为其状态包括ZoneId
,它不是时间字段,因此无法传递给Temporal.with(TemporalField, long)
。 ie. 即。 it is not possible to change the time-zone of a temporal class via the Temporal
interface. 无法通过Temporal
接口更改时态类的时区。
Given that ZonedDateTime
includes all the possible date-time fields, this restriction has little effect in practice. 鉴于ZonedDateTime
包含所有可能的日期时间字段,此限制在实践中几乎没有影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.