写一段java代码,实现根据字符串格式的起始和截至时间,获取时间范围内的每一天
时间: 2024-04-29 15:24:29 浏览: 51
```
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class DateRange {
public static List<LocalDate> getDateRange(String startDateStr, String endDateStr) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate startDate = LocalDate.parse(startDateStr, formatter);
LocalDate endDate = LocalDate.parse(endDateStr, formatter);
List<LocalDate> dateList = new ArrayList<>();
while (!startDate.isAfter(endDate)) {
dateList.add(startDate);
startDate = startDate.plusDays(1);
}
return dateList;
}
public static void main(String[] args) {
String startDateStr = "2021-01-01";
String endDateStr = "2021-01-10";
List<LocalDate> dateRange = getDateRange(startDateStr, endDateStr);
for (LocalDate date : dateRange) {
System.out.println(date);
}
}
}
```
输出结果:
```
2021-01-01
2021-01-02
2021-01-03
2021-01-04
2021-01-05
2021-01-06
2021-01-07
2021-01-08
2021-01-09
2021-01-10
```
阅读全文