背景介绍
给出两个时间,希望算出两者之间连续的日期,比如时间A:2023-10-01 00:00:00 时间B:2023-11-30 23:59:59,期望得到的连续日期为2023-10-01、2023-10-02、… 2023-11-30
Java版代码示例
import java.time.temporal.ChronoUnit;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.ArrayList;public static void main(String[] args) {List<String> continueDateList = new ArrayList<>();String startTime = "2023-12-11 00:00:00";String endTime = "2024-02-30 23:59:59";LocalDate start = LocalDate.parse(startTime,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));LocalDate end = LocalDate.parse(endTime,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
long days= ChronoUnit.DAYS.between(start,end);
log.info("days is {}",days);
for (int i=0;i<days+1;i++) {String startDate = start.plusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")).replaceAll("-", "").substring(0, 8);continueDateList.add(startDate);}
}