传入起始时间和结束时间就能获取其中的每一天
public static List<String> findDates(String beginTime, String endTime)
throws ParseException {
List<String> allDate = new ArrayList();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dBegin = sdf.parse(beginTime);
Date dEnd = sdf.parse(endTime);
allDate.add(sdf.format(dBegin));
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
while (dEnd.after(calBegin.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
allDate.add(sdf.format(calBegin.getTime()));
}
System.out.println("时间==" + allDate);
return allDate;
}
本文介绍如何使用Java通过SimpleDateFormat解析给定的开始和结束时间,然后按天生成并返回一个日期列表,适合处理时间区间内的日期操作。
2827

被折叠的 条评论
为什么被折叠?



