springboot获取近周的日期和对应星期几的源码
时间: 2024-01-22 13:18:40 浏览: 153
基于SpringBoot和OpenAI的聊天机器人设计源码
可以使用Java 8的新日期时间API来获取近周的日期和对应星期几。下面是一个示例代码:
```java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class WeekDates {
public static void main(String[] args) {
List<String> datesAndWeekdays = getDatesAndWeekdaysOfCurrentWeek();
for (String dateAndWeekday : datesAndWeekdays) {
System.out.println(dateAndWeekday);
}
}
public static List<String> getDatesAndWeekdaysOfCurrentWeek() {
List<String> datesAndWeekdays = new ArrayList<>();
LocalDate today = LocalDate.now();
LocalDate startOfWeek = today.with(DayOfWeek.MONDAY);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd E");
for (int i = 0; i < 7; i++) {
LocalDate date = startOfWeek.plusDays(i);
String dateAndWeekday = date.format(formatter);
datesAndWeekdays.add(dateAndWeekday);
}
return datesAndWeekdays;
}
}
```
该代码会输出当前周的日期和对应星期几,例如:
```
2021-10-04 Mon
2021-10-05 Tue
2021-10-06 Wed
2021-10-07 Thu
2021-10-08 Fri
2021-10-09 Sat
2021-10-10 Sun
```
阅读全文