Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why LocalDate.plusDays not working here?

Tags:

java

localdate

I'm trying to split a date range into individual dates in the following way:

private static void splitDates(LocalDate dateFrom, LocalDate dateTo) {
    while (dateFrom.isBefore(dateTo) || dateFrom.isEqual(dateTo)) {
        System.out.println(dateFrom);
        dateFrom.plusDays(1L);
    }
}

And I don't know why dateFrom.plusDays(1L) is not working as the date remains still the same so the loop becomes infinite.

like image 367
Arcones Avatar asked Nov 21 '25 09:11

Arcones


1 Answers

plusDays doesn't alter the original LocalDate, you have to assign the result :

dateFrom = dateFrom.plusDays(1L);
like image 196
Arnaud Avatar answered Nov 24 '25 01:11

Arnaud