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

后端 未结 22 1991
余生分开走
余生分开走 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: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 dates = new ArrayList();
    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 :)

提交回复
热议问题