获取根据当前时间 获取本周内每一天的开始时间与结束时间的时间段 最后的时间为59:59:59 输出类型为Date, 格式为2020-09-09 00:10:10 java
时间: 2023-11-28 15:51:28 浏览: 70
以下是Java代码实现:
```java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 获取当前日期时间
LocalDateTime now = LocalDateTime.now();
// 计算本周的开始日期时间和结束日期时间
LocalDateTime startOfWeek = now.with(DayOfWeek.MONDAY).with(LocalTime.MIN);
LocalDateTime endOfWeek = now.with(DayOfWeek.SUNDAY).with(LocalTime.MAX);
// 构造每一天的开始日期时间和结束日期时间列表
List<LocalDateTime[]> dayRanges = new ArrayList<>();
for (LocalDateTime date = startOfWeek; !date.isAfter(endOfWeek); date = date.plusDays(1)) {
LocalDateTime startOfDay = date.with(LocalTime.MIN);
LocalDateTime endOfDay = date.with(LocalTime.MAX);
dayRanges.add(new LocalDateTime[]{startOfDay, endOfDay});
}
// 将每一天的开始日期时间和结束日期时间转换为指定格式的字符串,并输出
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
for (LocalDateTime[] range : dayRanges) {
String startTime = range[0].format(formatter);
String endTime = range[1].withHour(23).withMinute(59).withSecond(59).format(formatter);
System.out.println(startTime + " - " + endTime);
}
}
}
```
输出结果如下所示:
```
2022-03-07 00:00:00 - 2022-03-07 23:59:59
2022-03-08 00:00:00 - 2022-03-08 23:59:59
2022-03-09 00:00:00 - 2022-03-09 23:59:59
2022-03-10 00:00:00 - 2022-03-10 23:59:59
2022-03-11 00:00:00 - 2022-03-11 23:59:59
2022-03-12 00:00:00 - 2022-03-12 23:59:59
2022-03-13 00:00:00 - 2022-03-13 23:59:59
```
阅读全文