public static List<String> findDaysStr(String cntDateBeg, String cntDateEnd) {
List<String> list = new ArrayList<>();
//拆分成数组
String[] dateBegs = cntDateBeg.split("-");
String[] dateEnds = cntDateEnd.split("-");
//开始时间转换成时间戳
Calendar start = Calendar.getInstance();
start.set(Integer.valueOf(dateBegs[0]), Integer.valueOf(dateBegs[1]) - 1, Integer.valueOf(dateBegs[2]));
Long startTIme = start.getTimeInMillis();
//结束时间转换成时间戳
Calendar end = Calendar.getInstance();
end.set(Integer.valueOf(dateEnds[0]), Integer.valueOf(dateEnds[1]) - 1, Integer.valueOf(dateEnds[2]));
Long endTime = end.getTimeInMillis();
//定义一个一天的时间戳时长
Long oneDay = 1000 * 60 * 60 * 24L;
Long time = startTIme;
//循环得出
while (time <= endTime) {
list.add(new SimpleDateFormat("yyyy-MM-dd").format(new Date(time)));
time += oneDay;
}
return list;
}
示例:
public static void main(String[] args) throws Exception {
System.out.println(findDaysStr("2021-09-01", "2021-09-08"));
}
结果:
[2021-09-01, 2021-09-02, 2021-09-03, 2021-09-04, 2021-09-05, 2021-09-06, 2021-09-07, 2021-09-08]