Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why YearMonth is Temporal while MonthDay is not Temporal?

Tags:

java

java-time

Why YearMonth class is Temporal (interface) while MonthDay is not Temporal?

This seems strange, because both classes are almost identical in their meaning.

I may suggest that MonthDay is never used anywhere where Temporal interface is used, but maybe there is a better reason.

like image 976
Code Complete Avatar asked Oct 18 '25 22:10

Code Complete


1 Answers

This is a question of reading the documentation of MonthDay, with thanks to davidxxx and others for finding the right quote:

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.

You may read this in conjunction with this snippet from the documentation of Temporal:

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

They have not wanted to define plus and minus for MonthDay. Because for example it’s not clear what February 28 plus 1 day is. In some years it is February 29, in other years it’s March 1.

In comparison, adding a month or a year to a YearMonth always gives a well-defined and not surprising result.

Links

  • Documentation of Temporal
  • Documentation of MonthDay
like image 186
Ole V.V. Avatar answered Oct 20 '25 11:10

Ole V.V.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!