This question already has an answer here:
I have two variables:
startDate
- for example 29/04/2018howManyDays
- for example 30
I want to get list of 30 days from date 29/04/2018. Can you tell me how can I do it? I found only days beetwen two dates.
int days = Days.daysBetween(startDate, endDate).getDays();
List<LocalDate> dates = new ArrayList<LocalDate>(days); // Set initial capacity to `days`.
for (int i=0; i < days; i++) {
LocalDate d = startDate.withFieldAdded(DurationFieldType.days(), i);
dates.add(d);
}
I don’t know how to change the code.
plusDays should do the trick
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:
Output is:
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: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
tohowManyDays
in the second and third lines.Links
java.time
.You can use this sample code to get 30 dates, starting from a specific date.
Run with
howManyDays=5
gives: