user3288346 user3288346 - 10 months ago 45
Java Question

How to build a list of LocalDate from a given range?

I have the following method, where I have the

and the
of type String
. I want to return a

protected List<LocalDate> getDateList(String startDate, String endDate) {

//build list here


How can I do this in Java 8?

Answer Source

Assuming that your input data is correct, we could use a Stream API to generate all dates in the given range:

final LocalDate start = LocalDate.parse(startDate, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
final LocalDate end = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy/MM/dd"));

final int days = (int) start.until(end, ChronoUnit.DAYS);

return Stream.iterate(start, d -> d.plusDays(1))


getDateList("2012/10/10", "2012/10/12")

[2012-10-10, 2012-10-11]

If you want to include the ending date, you need to use .limit(days + 1).