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

后端 未结 22 2020
余生分开走
余生分开走 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

    Something like this should definitely work:

    private List getListOfDaysBetweenTwoDates(Date startDate, Date endDate) {
        List result = new ArrayList();
        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;
    }
    

提交回复
热议问题