为什么 YearMonth 是 Temporal 而 MonthDay 不是 Temporal?

Why YearMonth is Temporal while MonthDay is not Temporal?

为什么 YearMonth class 是 Temporal(接口)而 MonthDay 不是 Temporal?

这看起来很奇怪,因为两个 class 的含义几乎相同。

我可能会建议永远不要在使用 Temporal 接口的任何地方使用 MonthDay,但也许有更好的理由。

这是阅读 MonthDay 文档的问题,感谢 davidxxx 和其他人找到正确的引用:

This class implements TemporalAccessor rather than Temporal. This is because it is not possible to define whether February 29th is valid or not without external information, preventing the implementation of plus/minus. Related to this, MonthDay only provides access to query and set the fields MONTH_OF_YEAR and DAY_OF_MONTH.

您可以结合 Temporal:

文档中的此片段阅读此内容

This is the base interface type for date, time and offset objects that are complete enough to be manipulated using plus and minus.

他们不想为 MonthDay 定义加号和减号。因为例如不清楚 2 月 28 日加 1 天是什么。有些年份是 2 月 29 日,有些年份是 3 月 1 日。

相比之下,在 YearMonth 上加上一个月或一年总是会得到 well-defined,这并不奇怪。

链接