HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 9 / Java – Stream of Dates

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?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Amol

    November 9, 2019

    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

      November 9, 2019

      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);
      	}
      }
      
Comments are closed on this article!

Search Tutorials

Java 9 Tutorial

  • Java 9 – Introduction
  • Java 9 – Compact Strings
  • Java 9 – Modules
  • Java 9 – JShell
  • Java 9 – Stream of Dates
  • Java 9 – Stream API Improvements
  • Java 9 – Immutable Collections
  • Java 9 – Interface Private Methods

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces