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

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);
        }
    }
}

 

上一篇:Markdown测试


下一篇:SimpleDateFormat