LocalDateTime获取本周周一到周日的时间
时间: 2023-11-28 15:44:42 浏览: 139
以下是获取本周周一到周日的时间的代码示例:
```java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// 获取当前日期时间
LocalDateTime now = LocalDateTime.now();
// 获取本周周一的日期
LocalDate monday = now.toLocalDate().with(DayOfWeek.MONDAY);
// 获取本周周日的日期
LocalDate sunday = now.toLocalDate().with(DayOfWeek.SUNDAY);
// 构造本周周一的日期时间
LocalDateTime mondayDateTime = monday.atStartOfDay();
// 构造本周周日的日期时间
LocalDateTime sundayDateTime = sunday.atTime(23, 59, 59);
// 格式化输出
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println("本周周一:" + mondayDateTime.format(formatter));
System.out.println("本周周日:" + sundayDateTime.format(formatter));
}
}
```
输出结果如下:
```
本周周一:2022-02-28 00:00:00
本周周日:2022-03-06 23:59:59
```
阅读全文