对日期区间进行遍历(步长为一天为例)
2021-01-05 本文已影响0人
SILENCE_SPEAKS
对日期区间进行遍历(步长为一天为例)
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date startDate = null;
Date endDate = null;
try {
startDate = format.parse("2020-10-01");
endDate = format.parse("2020-12-12");
} catch (ParseException e) {
e.printStackTrace();
}
if (startDate != null && endDate != null){
Date startDateFlag = startDate;
while (startDateFlag.compareTo(endDate) <= 0){
String startDateString = format.format(startDateFlag);
System.out.println(startDateString);
c.setTime(startDateFlag);
// 当前日期加一天
c.add(Calendar.DATE, 1);
startDateFlag = c.getTime();
System.out.println(startDateFlag);
}
}
}