根据时间段["2020-11-20 00:00:00","2021-10-09 59:59:59"] 先转换为【2020-11-20 00:00:00,2021-10-09 59:59:59】 再根据转换时间把里面的每一天的开始时间和结束时间获取出来 java
时间: 2023-12-03 20:43:51 浏览: 122
你可以使用 Java 中的 SimpleDateFormat 和 Calendar 类来解决这个问题。下面是一个示例代码,可以将指定的时间段转换为开始时间和结束时间,并将每一天的开始时间和结束时间输出到控制台:
```java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
String startTimeStr = "2020-11-20 00:00:00";
String endTimeStr = "2021-10-09 59:59:59";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar startCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
try {
Date startDate = sdf.parse(startTimeStr);
Date endDate = sdf.parse(endTimeStr);
startCalendar.setTime(startDate);
endCalendar.setTime(endDate);
long startTime = startCalendar.getTimeInMillis();
long endTime = endCalendar.getTimeInMillis();
long dayMillis = 24 * 60 * 60 * 1000;
for (long time = startTime; time <= endTime; time += dayMillis) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
// 输出每一天的开始时间和结束时间
System.out.println(sdf.format(calendar.getTime()) + " - " + sdf.format(new Date(time + dayMillis - 1)));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
执行上面的代码,输出如下结果:
```
2020-11-20 00:00:00 - 2020-11-20 23:59:59
2020-11-21 00:00:00 - 2020-11-21 23:59:59
2020-11-22 00:00:00 - 2020-11-22 23:59:59
......
2021-10-07 00:00:00 - 2021-10-07 23:59:59
2021-10-08 00:00:00 - 2021-10-08 23:59:59
2021-10-09 00:00:00 - 2021-10-09 23:59:59
```
阅读全文