获取指定时间间隔内的日期集合

public class TimeTest {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = sdf.parse("2021-05-01");
        Date endTime = sdf.parse("2021-07-01");
        Calendar instance = Calendar.getInstance();
        instance.setTime(startTime);
        ArrayList<String> timeList = new ArrayList<>();
        while (!startTime.after(endTime)) {
            String time = sdf.format(startTime);
            instance.add(Calendar.DAY_OF_MONTH, +1);
            startTime=instance.getTime();
            timeList.add(time);
            System.out.println(time);
        }
    }
}

 

posted @ 2021-11-19 20:22  进击的小蔡鸟  阅读(53)  评论(0编辑  收藏  举报