LocalDateTime withYear() method in Java with Examples
The withYear() method of LocalDateTime class in Java is used to get a copy of this LocalDateTime with the year changed to the year passed as the parameter to this method. The remaining values of this LocalDateTime remains the same.
public LocalDateTime withYear(int year)
Parameter: This method accepts a single mandatory parameter year which specifies the year to be set in the resultant LocalDateTime instance. The value of this year can range from MIN_YEAR to MAX_YEAR.
Returns: The function returns a LocalDateTime instance with the year changed to the year passed as the parameter to this method. The remaining values of this LocalDateTime remains the same.
Exceptions: The function throws a DateTimeException if the year value is invalid.
Below programs illustrate the LocalDateTime.withYear() method:
Original LocalDateTime: 2018-11-30T10:35:17.833 New LocalDateTime: 1998-11-30T10:35:17.833
Original LocalDateTime: 2015-04-06T10:15:30 New LocalDateTime: +20129-04-06T10:15:30
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.