The other answers are fine. And it will probably take a while still until Java 9 is running on your Android phone, but for anyone else reading along I should like to provide the Java 9 (and later) code snippet:
LocalDate startDate = LocalDate.of(2018, Month.APRIL, 29);
int howManyDays = 30;
List<LocalDate> dates = startDate.datesUntil(startDate.plusDays(howManyDays))
.collect(Collectors.toList());
System.out.println(dates);
Output is:
[2018-04-29, 2018-04-30, 2018-05-01, 2018-05-02, 2018-05-03,
2018-05-04, 2018-05-05, 2018-05-06, 2018-05-07, 2018-05-08,
2018-05-09, 2018-05-10, 2018-05-11, 2018-05-12, 2018-05-13,
2018-05-14, 2018-05-15, 2018-05-16, 2018-05-17, 2018-05-18,
2018-05-19, 2018-05-20, 2018-05-21, 2018-05-22, 2018-05-23,
2018-05-24, 2018-05-25, 2018-05-26, 2018-05-27, 2018-05-28]
As I already said in a comment, prefer java.time
over Joda-Time (that the snippet you had found for your question was using). java.time
is the modern Java date and time API. The Joda-Time home page says:
Note that Joda-Time is considered to be a largely “finished” project.
No major enhancements are planned. If using Java SE 8, please migrate
to java.time
(JSR-310).
If you did want to use the snippet from your question, all you would have to do was delete the first line and change days
to howManyDays
in the second and third lines.
Links
- Oracle tutorial: Date Time explaining how to use
java.time
.
- Joda-Time home page