Reputation: 15490
I am trying to get a list of dates in scala
val savePoint:java.util.Date= //olderDate
var days = List[String]()
for (date<-savePoint to java.util.Date.parse("yyyy-MM-dd") by date.plusDays(1)){
days::=date
}
but getting error
value to is not a member of java.util.Date
Upvotes: 3
Views: 753
Reputation: 339659
Using java.time.LocalDate
, produce a stream by calling datesUntil
, collected into a List
.
In Java syntax (I don't know Scala):
LocalDate // Represent a date-only value, without time-of-day, and without offset-from-UTC or time zone.
.of( 2019 , Month.SEPTEMBER , 22 ) // Specify you date in the past.
.dateUntil( // Generate a Stream of LocalDate objects.
LocalDate.now( ZoneId.of( "Africa/Tunis" ) ) // Capture the current date as seen through the wall-clock time used by the people of a particular region (a time zone).
) // Returns a `Stream< LocalDate >`.
.collect ( // Collects the items provided by the stream.
Collectors.toUnmodifiableList () // Instantiates an unmodifiable `List` of some indeterminate concrete class.
) // Returns a `List` holding `LocalDate` objects.
.toString() // Generates a textual listing of the collected dates using standard ISO 8601 format.
[2019-09-22, 2019-09-23, 2019-09-24, 2019-09-25, 2019-09-26, 2019-09-27, 2019-09-28, 2019-09-29, 2019-09-30, 2019-10-01, 2019-10-02, 2019-10-03]
The modern solution uses the java.time classes, specifically LocalDate
.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone or offset-from-UTC.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument. If you want to use the JVM’s current default time zone, make your intention clear by calling ZoneId.systemDefault()
. If critical, confirm the zone with your user.
Specify a proper time zone name in the format of Continent/Region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 2-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the code becomes ambiguous to read in that we do not know for certain if you intended to use the default or if you, like so many programmers, were unaware of the issue.
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
Or specify a date. You may set the month by a number, with sane numbering 1-12 for January-December.
LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ; // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.
Or, better, use the Month
enum objects pre-defined, one for each month of the year. Tip: Use these Month
objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety. Ditto for Year
& YearMonth
.
LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;
LocalDate::datesUntil
➙ Stream of LocalDate
objectsYou can accomplish your goal in a one-liner, by calling LocalDate::datesUntil
to generate a stream that can be collected into a list.
ZoneId z = ZoneId.of ( "America/Edmonton" );
LocalDate today = LocalDate.now ( z );
LocalDate then = LocalDate.of ( 2019 , Month.SEPTEMBER , 22 );
List < LocalDate > dates = then.datesUntil ( today ).collect ( Collectors.toUnmodifiableList () );
Dump to console.
System.out.println ( "From: " + then + " to: " + today + " is: " + dates );
From: 2019-09-22 to: 2019-10-04 is: [2019-09-22, 2019-09-23, 2019-09-24, 2019-09-25, 2019-09-26, 2019-09-27, 2019-09-28, 2019-09-29, 2019-09-30, 2019-10-01, 2019-10-02, 2019-10-03]
org.threeten.extra.LocalDateRange
FYI, to represent a span-of-time between a pair of dates, you can use the LocalDateRange
class from the ThreeTen-Extra project.
LocalDateRange range = LocalDateRange.of( then , today ) ;
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.
Upvotes: 2
Reputation: 2095
For Scala, you can use Lamma Date (http://www.lamma.io)
Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_71).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import io.lamma._
import io.lamma._
scala> Date(2015, 7, 1) to Date.today() foreach println
Date(2015,7,1)
Date(2015,7,2)
Date(2015,7,3)
Date(2015,7,4)
Date(2015,7,5)
Date(2015,7,6)
Date(2015,7,7)
Upvotes: 0