Any better/simple solution to do this ?
Yes. Functionality now built into Java 9 and later.
java.time.LocalDate::datesUntil ➙ Stream of
The modern approach uses java.time classes.
java.time.LocalDate has built-in support for generating a stream of dates. Call
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]
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
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
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
YearQuarter, and more.