java 获取本周起止时间 返回Date
时间: 2023-04-01 17:04:08 浏览: 97
可以使用以下代码获取本周的起止时间:
```java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate startOfWeek = today.with(DayOfWeek.MONDAY);
LocalDate endOfWeek = today.with(DayOfWeek.SUNDAY);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String startOfWeekStr = startOfWeek.format(formatter);
String endOfWeekStr = endOfWeek.format(formatter);
System.out.println("本周起始时间:" + startOfWeekStr);
System.out.println("本周结束时间:" + endOfWeekStr);
}
}
```
输出结果如下:
```
本周起始时间:2022-02-28
本周结束时间:2022-03-06
```
阅读全文