The withMonth() method of LocalDate class in Java returns a copy of this LocalDate with the month-of-year altered.
public LocalDate withMonth(int month)
Parameter: This method accepts a mandatory parameter month which specifies the month-of-year to set in the result, from 1 i.e., January to 12 i.e., December.
Returns: The function returns a LocalDate based on this date with the requested month, not null.
Exceptions: The function throws a DateTimeException when the month-of-year value is invalid.
Below programs illustrate the LocalDate.withMonth() method:
The date with month is: 2018-01-07
java.time.DateTimeException: Invalid value for MonthOfYear (valid values 1 - 12): 13
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.