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

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

    Get the number of days between dates, inclusive.

    public static List getDaysBetweenDates(Date startdate, Date enddate)
    {
        List dates = new ArrayList();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(startdate);
    
        while (calendar.getTime().before(enddate))
        {
            Date result = calendar.getTime();
            dates.add(result);
            calendar.add(Calendar.DATE, 1);
        }
        return dates;
    }
    

提交回复
热议问题