I want to have a continuous date sequence like ['2014-01-01','2014-01-02', ...]

Then I define a stream to do that.

def daySeq(start: Date): Stream[Date] = Stream.cons(start,  daySeq(plusDays(start, 1)))

I get the sequence within range [start, end) by calling

daySeq(from).takeWhile(_.getTime < to.getTime)

Any better/simple solution to do this ?


2 Answers 2


I suggest using Joda-Time's LocalDate instead of Java's Date to represent dates without a time zone.

Assuming you only need to traverse the days once, use an Iterator instead of a Stream.

def dayIterator(start: LocalDate, end: LocalDate) = Iterator.iterate(start)(_ plusDays 1) takeWhile (_ isBefore end)

Example usage:

dayIterator(new LocalDate("2013-10-01"), new LocalDate("2014-01-30")).foreach(println)

If you do need a lazily-evaluated list, then Stream is appropriate. I suggest using iterate instead of cons in that case.

def dayStream(start: LocalDate, end: LocalDate) = Stream.iterate(start)(_ plusDays 1) takeWhile (_ isBefore end)
  • \$\begingroup\$ iterate looks better and simpler \$\endgroup\$
    – jilen
    Mar 24, 2014 at 1:22
  • 1
    \$\begingroup\$ FYI: The Joda-Time project is now in maintenance mode, its creator, Stephen Colebourne, having gone on to lead JSR 310 defining the java.time classes built into Java 8+. See Tutorial by Oracle. \$\endgroup\$ Oct 4, 2019 at 22:02
  • \$\begingroup\$ This works with the java.time classes if you replace new LocalDate with LocalDate.parse \$\endgroup\$
    – DavidP
    Jan 3, 2020 at 14:17

Any better/simple solution to do this ?

Yes. Functionality now built into Java 9 and later.

java.time.LocalDate::datesUntil ➙ Stream of LocalDate objects

The modern approach uses java.time classes.

The java.time.LocalDate has built-in support for generating a stream of dates. Call LocalDate::datesUntil.

I do not know Scala, so here is Java syntax.

LocalDate today = LocalDate.now ( ZoneId.of ( "Africa/Tunis" ) );
LocalDate later = today.plusDays ( 3 );
List < LocalDate > dates = today.datesUntil ( later ).collect ( Collectors.toUnmodifiableList () );

[2019-10-04, 2019-10-05, 2019-10-06]

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

  • 1
    \$\begingroup\$ It's worth noting that the LocalDate::datesUntil method was one of the "minor features" added in Java 9. This means that it's not available if you're using an environment that is running on Java 8, which many things are since it's an LTS release. \$\endgroup\$
    – DavidP
    Jan 3, 2020 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.