/**
* 获取两个指定日期之间的其他日期
*/
public class BetweenDateDemo {
public static void main(String[] args) {
String startTime = "2024-03-10";
String endTime = "2024-03-20";
List<String> list = getBetweenDate(startTime, endTime);
System.out.println(list);
}
public static List<String> getBetweenDate(String startTime, String endTime){
List<String> list = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date startDate = sdf.parse(startTime);
Date endDate = sdf.parse(endTime);
Calendar cal = Calendar.getInstance();
//判断此日期是否在指定日期之后
while (startDate.getTime() <= endDate.getTime()) {
//把日期添加到集合
list.add(sdf.format(startDate));
//使用给定的Date设置此Calendar的时间
cal.setTime(startDate);
//根据日历的规则,为指定的日历字段添加或减去指定的时间量
cal.add(Calendar.DAY_OF_MONTH, 1);
//获取增加后的日期
startDate = cal.getTime();
}
} catch (ParseException e) {
e.printStackTrace();
}
return list;
}
}
输出

990




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



