Massin
Massin

Reputation: 117

Roll over the month of dates using LocalDate in Java?

I am designing a booking system in java that only has to handle dates for the year 2015 and using the LocalDate class, a booking consists of a start date and a duration e.g. 2015, 5, 26: 7 would be a booking starting on 26 may 2015 for a duration of 7 days, {26,27,28,29,30,31} May and the {1}st Jun. If i am say generating my dates with a loop is there anyway to get the correct roll over of the month?, so the date won't say be 32nd of May but instead 1st Jun.

        int initialDate=26;
        int initialMonth=5; 
        int duration= 7; 
        int endDate= initialDate+duration; 
        LocalDate date; 

        while(initialDate<=endDate){
            date=LocalDate.of(2015, initialMonth, initialDate); 
            System.out.println(date.getDayOfMonth());
            initialDate++; 
        }

Upvotes: 0

Views: 948

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

Assuming you're using Java 8, why not use LocalDate#plusDays?

LocalDate startDate = LocalDate.of(2015, 5, 26);
LocalDate endDate = startDate.plusDays(7);

System.out.println(endDate);

Which outputs 2015-06-02

Upvotes: 3

Related Questions