Java – Stream of Dates

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 !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “Java – Stream of Dates”

  1. 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

    Reply
    • Try editing this program as per your need.

      import java.time.LocalDateTime;
      import java.time.temporal.ChronoUnit;
      import java.util.Iterator;
      import java.util.stream.Stream;
      
      public class Main
      {
      	public static void main(String[] args) 
      	{
      		DateTimeRange range = new DateTimeRange(LocalDateTime.now(), 
      				LocalDateTime.now().plusDays(1));
      		
      		range.stream().forEach(System.out::println);
      	}
      }
      
      class DateTimeRange 
      		implements Iterable&lt;LocalDateTime&gt; 
      {
      	private final LocalDateTime startDateTime;
      	private final LocalDateTime endDateTime;
      
      	public DateTimeRange(LocalDateTime sdt, 
      				LocalDateTime edt) {
      		this.startDateTime = sdt;
      		this.endDateTime = edt;
      	}
      
      	@Override
      	public Iterator&lt;LocalDateTime&gt; iterator() {
      		return stream().iterator();
      	}
      
      	public Stream&lt;LocalDateTime&gt; stream() 
      	{
      		return Stream.iterate(startDateTime, d -&gt; d.plusSeconds(1))
      			.limit(ChronoUnit.SECONDS.between(startDateTime, endDateTime) + 1);
      	}
      }
      
      Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.