Earlier quoted context omitted.
> Only other edge case is if someone takes a 2024-02-29 timestamp and modify the year part, without using date library to do so. Date library does not help here, for example, both Python and Java's date library [0][1] have only "days" in their timedelta/Duration constructor. Month and year are ambiguous. What would you do if you want someone's birthday this year? The most obvious way: birthday: datetime.date birthday…
The Java library actually supports this case. It may not have the convenient "years" method directly, but you can add an arbitrary unit of time quite easily Example with duration: Duration.of(1, ChronoUnit.YEARS) Example with datetime: today.plus(2, ChronoUnit.YEARS) It even supports decades, centuries, eras and half days :-) For those curious about how it handles leap years: var leapDay = LocalDate.of(2024, 2, 29) l…
This method adds the specified amount to the years field in three steps:
* Add the input years to the year field
* Check if the resulting date would be invalid
* Adjust the day-of-month to the last valid day if necessary
TIL. I didn't know that Java had a builtin calendar (instead of datetime) library.