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

后端 未结 22 1842
余生分开走
余生分开走 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条回答
  •  旧时难觅i
    2020-11-22 13:43

    Like as @folone, but correct

    private static List getDatesBetween(final Date date1, final Date date2) {
        List dates = new ArrayList<>();
        Calendar c1 = new GregorianCalendar();
        c1.setTime(date1);
        Calendar c2 = new GregorianCalendar();
        c2.setTime(date2);
        int a = c1.get(Calendar.DATE);
        int b = c2.get(Calendar.DATE);
        while ((c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR)) || (c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH)) || (c1.get(Calendar.DATE) != c2.get(Calendar.DATE))) {
            c1.add(Calendar.DATE, 1);
            dates.add(new Date(c1.getTimeInMillis()));
        }
        return dates;
    }
    

提交回复
热议问题