计算两个时间段内的所有时间

public static List<String> getDate(Date startDate, Date endDate){
//定义时间格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
List<String> list = new ArrayList<>();
// 转化成日期类型

//用Calendar 进行日期比较判断
Calendar calendar = Calendar.getInstance();
while (startDate.getTime()<=endDate.getTime()){
// 把日期添加到集合
list.add(simpleDateFormat.format(startDate));
// 设置日期
calendar.setTime(startDate);
//把日期增加一分
calendar.add(Calendar.DAY_OF_MONTH, 1);
// 获取增加后的日期
startDate=calendar.getTime();
}
return list;
}
posted @ 2023-01-14 09:27  初见如月  阅读(37)  评论(0编辑  收藏  举报