Data Stream Recommendation
In Java 9, you can use the following new method: LocalDate::datesUntil :
LocalDate start = LocalDate.of(2017, 2, 1); LocalDate end = LocalDate.of(2017, 2, 28); Stream<LocalDate> dates = start.datesUntil(end.plusDays(1)); List<LocalDate> list = dates.collect(Collectors.toList());
The new datesUntil(...) method works with an exceptional end date, so the hack shown is for adding a day.
After you receive the stream, you can use all the functions offered by java.util.stream - or java.util.function -packages. Working with streams has become so simple compared to previous approaches based on setting for- or while-loops.
Or if you are looking for a stream-based solution that is turned on by inclusive dates by default, but can also be configured differently, you might find the DateInterval class in my Time4J library interesting because it offers many special functions in date streams, including an execution delimiter which faster than in Java-9:
PlainDate start = PlainDate.of(2017, 2, 1); PlainDate end = start.with(PlainDate.DAY_OF_MONTH.maximized()); Stream<PlainDate> stream = DateInterval.streamDaily(start, end);
Or even simpler in the case of full months:
Stream<PlainDate> februaryDates = CalendarMonth.of(2017, 2).streamDaily(); List<LocalDate> list = februaryDates.map(PlainDate::toTemporalAccessor).collect(Collectors.toList());
Meno Hochschild Feb 20 '17 at 12:08 2017-02-20 12:08
source share