how to get a list of dates between two dates in java

后端 未结 22 1512
余生分开走
余生分开走 2020-11-22 13:24

I want a list of dates between start date and end date.

The result should be a list of all dates including the start and end date.

相关标签:
22条回答
  • 2020-11-22 13:31
    List<LocalDate> totalDates = new ArrayList<>();
    popularDatas(startDate, endDate, totalDates);
    System.out.println(totalDates);
    
    private void popularDatas(LocalDate startDate, LocalDate endDate, List<LocalDate> datas) {
        if (!startDate.plusDays(1).isAfter(endDate)) {
            popularDatas(startDate.plusDays(1), endDate, datas);
        } 
        datas.add(startDate);
    }
    

    Recursive solution

    0 讨论(0)
  • 2020-11-22 13:36

    With Lamma it looks like this in Java:

        for (Date d: Dates.from(2014, 6, 29).to(2014, 7, 1).build()) {
            System.out.println(d);
        }
    

    and the output is:

        Date(2014,6,29)
        Date(2014,6,30)
        Date(2014,7,1)
    
    0 讨论(0)
  • 2020-11-22 13:37

    Streams

    Edit: Joda-Time is now deprecated, changed the answer to use Java 8 instead.

    Here is the Java 8 way, using streams.

    List<LocalDate> daysRange = Stream.iterate(startDate, date -> date.plusDays(1)).limit(numOfDays).collect(Collectors.toList());
    
    0 讨论(0)
  • 2020-11-22 13:37

    Something like this should definitely work:

    private List<Date> getListOfDaysBetweenTwoDates(Date startDate, Date endDate) {
        List<Date> result = new ArrayList<Date>();
        Calendar start = Calendar.getInstance();
        start.setTime(startDate);
        Calendar end = Calendar.getInstance();
        end.setTime(endDate);
        end.add(Calendar.DAY_OF_YEAR, 1); //Add 1 day to endDate to make sure endDate is included into the final list
        while (start.before(end)) {
            result.add(start.getTime());
            start.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }
    
    0 讨论(0)
  • 2020-11-22 13:37

    You can also look at the Date.getTime() API. That gives a long to which you can add your increment. Then create a new Date.

    List<Date> dates = new ArrayList<Date>();
    long interval = 1000 * 60 * 60; // 1 hour in millis
    long endtime = ; // create your endtime here, possibly using Calendar or Date
    long curTime = startDate.getTime();
    while (curTime <= endTime) {
      dates.add(new Date(curTime));
      curTime += interval;
    }
    

    and maybe apache commons has something like this in DateUtils, or perhaps they have a CalendarUtils too :)

    EDIT

    including the start and enddate may not be possible if your interval is not perfect :)

    0 讨论(0)
  • 2020-11-22 13:41

    Back in 2010, I suggested to use Joda-Time for that.

    Note that Joda-Time is now in maintenance mode. Since 1.8 (2014), you should use java.time.

    Add one day at a time until reaching the end date:

    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);
    }
    

    It wouldn't be too hard to implement your own iterator to do this as well, that would be even nicer.

    0 讨论(0)
提交回复
热议问题