LocalDate withDayOfMonth() method in Java with Examples
The withDayOfMonth() method of LocalDate class in Java returns a copy of this LocalDate with the day-of-month altered.
public LocalDate withDayOfMonth(int dayOfMonth)
Parameter: This method accepts a mandatory parameter dayOfMonth whicthe day-of-month to set in the result, from 1 to 28-31.
Returns: The function returns a LocalDate based on this date with the requested day, not null.
Exceptions: The function throws a DateTimeException when the day of the month value is invalid.
Below programs illustrate the LocalDate.withDayOfMonth() method:
The date with day of the month is: 2018-12-01
java.time.DateTimeException: Invalid value for DayOfMonth (valid values 1 - 28/31): 35
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.