I am trying to get a monthly recurrence dates for a specific period. The code below works except when I am specifying the recurrence date of 30. Then it is generating the following error, which is normal:
Exception in thread "main" java.time.DateTimeException: Invalid date 'FEBRUARY 30'
My question is : Is there a way to set the recurrence to the last day of the month if the specified date (day) of the recurrence is higher than the last day of the month ?
Thanks !
public class MonthlyRecurrence {
    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2020, 10, 6);
        LocalDate endDate = LocalDate.of(2021, 12, 31);
        int dayOfMonth = 30;
        List<LocalDate> reportDates = getReportDates(startDate, endDate, dayOfMonth);
        System.out.println(reportDates);
    }
    private static List<LocalDate> getReportDates(LocalDate startDate, LocalDate endDate, int dayOfMonth) {
        List<LocalDate> dates = new ArrayList<>();
        LocalDate reportDate = startDate;
        
        while (reportDate.isBefore(endDate)) {
            reportDate = reportDate.plusMonths(1).withDayOfMonth(dayOfMonth);
            dates.add(reportDate);
        }
        return dates;
    }
    }