Date and Time handling always been a pain area for java developers. The new Date-Time API added in Java 8 changed the way, you interact with dates in java. It was a very powerful and much-needed improvement. The only thing missing was, getting a stream of dates having some common difference between two subsequent dates (though it was possible but there was no easy way).
Java 9 has introduced a new method LocalDate.datesUntil()
which can give a stream on dates. Using datesUntil()
makes it easy to create dates streams with fixed offset.
1. Syntax of LocalDate.datesUntil()
This method has two overloaded forms:
Stream<LocalDate> datesUntil(LocalDate end) Stream<LocalDate> datesUntil(LocalDate end, Period step)
The first version (i.e. without a Period
) internally calls the second method with Period.ofDays(1)
and generates stream of dates with difference of 1 day in between.
Date Stream Example with LocalDate.datesUntil()
Creating a stream of dates is very simple and straightforward.
import java.time.LocalDate; import java.time.Period; import java.util.List; import java.util.stream.Collectors; public class Java9StreamExamples { public static void main(String[] args) { System.out.println( getDaysInJava9(LocalDate.now(), LocalDate.now().plusDays(10)) ); System.out.println( getDaysInJava9Weeks(LocalDate.now(), LocalDate.now().plusWeeks(10)) ); } //Stream of dates with 1 day difference public static List<LocalDate> getDaysInJava9(LocalDate start, LocalDate end) { return start.datesUntil(end).collect(Collectors.toList()); } //Stream of dates with 1 week difference public static List<LocalDate> getDaysInJava9Weeks(LocalDate start, LocalDate end) { return start.datesUntil(end, Period.ofWeeks(1)).collect(Collectors.toList()); } } Output: [2017-07-31, 2017-08-01, 2017-08-02, 2017-08-03, 2017-08-04, 2017-08-05, 2017-08-06, 2017-08-07, 2017-08-08, 2017-08-09] [2017-07-31, 2017-08-07, 2017-08-14, 2017-08-21, 2017-08-28, 2017-09-04, 2017-09-11, 2017-09-18, 2017-09-25, 2017-10-02]
2. Date Stream in Java 8
If you have still not adapted Java 9, then you can use given below method to generate Date streams. This code is compatible to Java 8.
import java.time.LocalDate; import java.time.Period; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class Java9StreamExamples { public static void main(String[] args) { System.out.println( getDaysInJava8(LocalDate.now(), 10) ); } //Stream of dates with 1 day difference public static List<LocalDate> getDaysInJava8(LocalDate start, int days) { return Stream.iterate(start, date -> date.plusDays(1)) .limit(days) .collect(Collectors.toList()); } } Output: [2017-07-31, 2017-08-01, 2017-08-02, 2017-08-03, 2017-08-04, 2017-08-05, 2017-08-06, 2017-08-07, 2017-08-08, 2017-08-09]
Drop me your questions in the comments section.
Happy Learning !!
Amol
Hi Lokesh,
I want to create a sequential date time generator which is required for performance testing within a boundary of start date and end date.
For e.g. Start Date: 2018-11-01 00:00:00 End Date: 2018-11-10 23:59:59
1st Value= 2018-11-01 00:00:00
2nd Value = 2018-11-01 00:00:01
3rd Value = 2018-11-01 00:00:02
–
–
–
61st Value = 2018-11-01 00:01:00
62nd Value = 2018-11-01 00:01:01
How do I create this using Java 8?
Can you help me with this?
Thanks,
Amol
Lokesh Gupta
Try editing this program as per your need.