简体   繁体   English

为什么ZonedDateTime类没有实现TemporalAdjuster接口

[英]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中的大多数类(例如LocalDateOffsetDateTime )实现了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.

相关问题 为什么@Autowired 会引发 UnsatisfiedDependencyException,甚至 class 也没有实现任何接口? - Why @Autowired raises UnsatisfiedDependencyException, even the class does not implement any interface? class中的接口方法没有实现吗? - Interface methods in a class that does not implement it? 为什么这个类编译即使它没有正确实现其接口? - Why does this class compile even though it does not properly implement its interface? 类 SpringHibernateJpaPersistenceProvider 没有实现请求的接口 PersistenceProvider - Class SpringHibernateJpaPersistenceProvider does not implement the requested interface PersistenceProvider 为什么抽象类要实现接口? - Why should abstract class implement an interface? 为什么类编译为.class但接口不编译为.interface - Why classes compile to .class but interface does not to .interface 为什么ExecutorService接口没有实现AutoCloseable? - Why does the ExecutorService interface not implement AutoCloseable? 为什么 ArrayList 实现了 RandomAccess 接口? - Why does ArrayList implement RandomAccess Interface? Map 接口是否扩展或实现了一些其他接口或 class? - Does Map interface extend or implement some other interface or class? 将最终类强制转换为该类未声明要实现的兼容接口 - Cast a final class to a compatible interface that the class does not claim to implement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM