Starting with JDK 8

First, starting with JDK 8, the dates can be easily defined as LocalDate, without the help of Calendar:

LocalDate startLocalDate = LocalDate.of(2019, 2, 1);
LocalDate endLocalDate = LocalDate.of(2019, 2, 21);

Once the start date is equal with the end date, we stop the loop via the LocalDate.isBefore(ChronoLocalDate other) method. This flag method checks if this date is before the given date.

Increasing the start date day by day until the end date can be accomplished using the LocalDate.plusDays(long daysToAdd) method. Using these two methods in a for loop results in the following code:

for (LocalDate date = startLocalDate; 
date.isBefore(endLocalDate); date = date.plusDays(1)) {

// do something with this day
System.out.println(date);
}

A snapshot of the output should be as follows:

2019-02-01
2019-02-02
2019-02-03
...
2019-02-20
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.39.144