java 获取上周的每一天日期
时间: 2023-07-18 19:47:33 浏览: 85
js获取当前周、上一周、下一周日期
下面是 Java 代码获取上周的每一天日期:
```java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
public class LastWeekDates {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate lastWeekStart = today.minusWeeks(1).with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
LocalDate lastWeekEnd = today.minusWeeks(1).with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
for (LocalDate date = lastWeekStart; !date.isAfter(lastWeekEnd); date = date.plusDays(1)) {
System.out.println(date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}
}
}
```
这个程序会输出上周每一天的日期,格式为 yyyy-MM-dd。你可以根据自己的需要修改日期格式和输出方式。
阅读全文