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.
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 thanTemporal
. 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 fieldsMONTH_OF_YEAR
andDAY_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