This method returns a new copy of this local date, with day-of-month field replaced with the provided 'dayOfMonth' value.
Using an invalid value for day will throw exception.
package com.logicbig.example.localdate;
import java.time.LocalDate;
public class WithDayOfMonthExample2 {
public static void main (String... args) {
LocalDate d = LocalDate.of(2016, 10, 20);
LocalDate d2 = d.withDayOfMonth(32);
System.out.println(d2);
}
}
Output
Caused by: java.time.DateTimeException: Invalid value for DayOfMonth (valid values 1 - 28/31): 32
at java.time.temporal.ValueRange.checkValidValue(ValueRange.java:311)
at java.time.temporal.ChronoField.checkValidValue(ChronoField.java:703)
at java.time.LocalDate.of(LocalDate.java:268)
at java.time.LocalDate.withDayOfMonth(LocalDate.java:1099)
at com.logicbig.example.localdate.WithDayOfMonthExample2.main(WithDayOfMonthExample2.java:15)
... 6 more