The withYear() method of LocalDate class in Java returns a copy of this LocalDate with the year altered.
public LocalDate withYear(int year)
Parameter: This method accepts a mandatory parameter year which specifies the year to set in the result which can be in the range from MIN_YEAR to MAX_YEAR.
Returns: The function returns a LocalDate based on this date with the requested year, not null.
Exceptions: The function throws a DateTimeException when the year value is invalid.
Below programs illustrate the LocalDate.withYear() method:
The date with year is: 2018-12-07
The date with year is: 2014-01-07
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.