plusYears method

LocalDate plusYears(
  1. int years
)

Returns a new LocalDate with years added.

Adjusts February 29 to February 28 if the new year is not a leap year.

Example:

final date = LocalDate(2023, 2, 29);
final result = date.plusYears(1); // 2024-02-29

Implementation

LocalDate plusYears(int years) {
  int newYear = year + years;
  int newDay = day;
  if (month == 2 && day == 29 && !_isLeapYear(newYear)) {
    newDay = 28;
  }
  return LocalDate(newYear, month, newDay);
}