//计算两个月的日期中间的日期
@Test
void test01(){
Date startTime;
Date endTime;
try {
startTime = new SimpleDateFormat("yyyy-MM-dd").parse("2022-08-23");
endTime= new SimpleDateFormat("yyyy-MM-dd").parse("2022-09-23");
} catch (ParseException e) {
throw new RuntimeException(e);
}
//定义一个接受时间的集合
List<Date> lDate = new ArrayList<Date>();
lDate.add(startTime);
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(startTime);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(endTime);
// 测试此日期是否在指定日期之后
DateFormat dformat=new SimpleDateFormat("yyyy-MM-dd");
ArrayList<String> dateList = new ArrayList<>();
while (endTime.after(calBegin.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
dateList.add(dformat.format(calBegin.getTime()));
}
System.out.println(dateList);
}
运行结果