1

I have two variables:

  • startDate - for example 29/04/2018
  • howManyDays - 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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • But you already have the code for doing exactly what you want, you just have `days` instead of `howManyDays`. Does "I found only" mean you only found an example for something similar and you couldn't figure out how to adapt it? – Matti Virkkunen Apr 29 '18 at 08:50
  • @MattiVirkkunen yes I don't know how to change it – haruki.phoenyx Apr 29 '18 at 08:51
  • Do you understand how this code works to begin with or are you just copying and pasting? If you read through the code, and understand it, you will know how to adapt it because it's already doing what you want to do. – Matti Virkkunen Apr 29 '18 at 08:53
  • Adding an external dependency for this job makes good sense since the built-in support for it (the outmoded `java.util.Calendar` class) is poorly designed. I would certainly pick [ThreeTen Backport](http://www.threeten.org/threetenbp/) so I could use [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). 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).” – Ole V.V. Apr 29 '18 at 09:12
  • Duplicate of: [*Return a list of dates given an interval of dates and days of the week as booleans*](https://stackoverflow.com/q/35923461/642706) and of [*how to get a list of dates between two dates in java*](https://stackoverflow.com/q/2689379/642706). Please search Stack Overflow thoroughly before posting. – Basil Bourque Apr 30 '18 at 21:17

3 Answers3

2
public static List<LocalDate> getLocalDates(String startdate, int days) {
        List<LocalDate> localDates = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate localDate = LocalDate.parse(startdate, formatter);
        for (int i = 1; i <= days; i++) {
            localDates.add(localDate.plusDays(i));

        }
        return localDates;

    }

plusDays should do the trick

Prashant Thorat
  • 361
  • 1
  • 5
  • 18
2

You can use this sample code to get 30 dates, starting from a specific date.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class Days {

    public static void main(String[] args) {
        String startDate = "29/04/2018";
        int howManyDays = 30;

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate localDate = LocalDate.parse(startDate, formatter);
        List<LocalDate> dates = new ArrayList<>();
        for (int i = 0; i < howManyDays; i++) {
            dates.add(localDate.plusDays(i));
        }

        //For check
        dates.forEach(System.out::println);
    }
}

Run with howManyDays=5 gives:

2018-04-29
2018-04-30
2018-05-01
2018-05-02
2018-05-03
Jagrut Sharma
  • 4,574
  • 3
  • 14
  • 19
1

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161