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– jilen2014年03月24日 01:22:17 +00:00Commented 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\$Basil Bourque– Basil Bourque2019年10月04日 22:02:12 +00:00Commented Oct 4, 2019 at 22:02
-
\$\begingroup\$ This works with the java.time classes if you replace
new LocalDate
withLocalDate.parse
\$\endgroup\$DavidP– DavidP2020年01月03日 14:17:50 +00:00Commented 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?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP....
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– DavidP2020年01月03日 14:04:43 +00:00Commented Jan 3, 2020 at 14:04