public class demo11 {
public static void main(String[] args) {
String start = "2020-08-08";
String end = "2021-09-08";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dStart = null;
Date dEnd = null;
try {
dStart = sdf.parse(start);
dEnd = sdf.parse(end);
} catch (ParseException e) {
e.printStackTrace();
}
List<Date> dateList = findDates(dStart, dEnd);
for (Date date : dateList) {
System.out.println(sdf.format(date));
}
}

//JAVA获取某段时间内的所有日期
public static List<Date> findDates(Date dStart, Date dEnd) {
Calendar cStart = Calendar.getInstance();
cStart.setTime(dStart);

List dateList = new ArrayList();
//别忘了,把起始日期加上
dateList.add(dStart);
// 此日期是否在指定日期之后
while (dEnd.after(cStart.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
cStart.add(Calendar.DAY_OF_MONTH, 1);
dateList.add(cStart.getTime());
}
return dateList;
}

}
posted on 2022-04-11 15:24  我叫福禄娃  阅读(45)  评论(0编辑  收藏  举报