JAVA获取两个日期之间的所有日期集合

 

 private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

 

 

public static List<String> splitDateList(Date startDate, Date endDate) {
        List<String> listDate = new ArrayList<>();
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            while (calendar.getTime().before(endDate) || calendar.getTime().equals(endDate)) {
                listDate.add(dateFormat.format(calendar.getTime()));
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
            return listDate;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listDate;
    }

 

posted @ 2021-07-26 17:39  yvioo  阅读(3302)  评论(0编辑  收藏  举报