Home > Back-end >  Getting the day for each week missing 31
Getting the day for each week missing 31

Time:12-21

I am calculating the same day for each week:

import java.time.LocalDate;
import java.util.List;
import java.util.ArrayList;
        
class HelloWorld {
    public static void main(String[] args) {
        LocalDate startLocalDate = LocalDate.parse("2023-01-03");
        LocalDate endLocalDate = startLocalDate.withDayOfMonth(startLocalDate.getMonth().length(startLocalDate.isLeapYear()));
        int startDayOfWeek = startLocalDate.getDayOfWeek().getValue();
        List<LocalDate> listOfDays = new ArrayList<LocalDate>();
            for (LocalDate date = startLocalDate; date.isBefore(endLocalDate); date = date.plusDays(1)) {
            if (date.getDayOfWeek().getValue() == startDayOfWeek) {
                listOfDays .add(date);
            }
        }
        System.out.println(listOfDays);
    }
}

I am getting:

[2023-01-03, 2023-01-10, 2023-01-17, 2023-01-24]

but I expected to get:

[2023-01-03, 2023-01-10, 2023-01-17, 2023-01-24, 2023-01-31]

31st is missing.

CodePudding user response:

Fencepost error. Your loop's 'end condition' is:

If the date variable is no longer before endDate, stop right now.

for (init; cond; incr) code;

is identical to:

init;
while (cond) {
  code;
  incr;
}

That means that once date is updated by the for increment (date = date.plusDays(1)), that new date is immediately checked with the condition (is that still BEFORE endDate), and the loop aborts then and there if that fails to hold.

Thus, your code goes for 31st of january and aborts the loop on the spot, it never gets handled.

Presumably, you want, for condition, not date.isBefore(endDate), but !date.isAfter(endLocalDate). To ensure that 31st of jan IS handled.

Alternatively, keep it as is, but edit your end date. This simplifies the calculation for end date in passing:

LocalDate endLocalDate = startLocalDate.withDayOfMonth(1).plusMonths(1);

Now endLocalDate is feb 1st, and that means jan 31st is still processed, and then feb 1st causes the loop to stop.

  •  Tags:  
  • java
  • Related