Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting the day for each week missing 31

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

31st is missing.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading