对于每个月的最后一天,请首先为一周中的某天创建一个列表集合-
List<DayOfWeek>dayOfWeek = new ArrayList<>();
现在,获取2019年每个月的最后一天的星期几,使用withYear()设置年份,并使用lastDayOfMonth()和getDayOfWeek方法-
for (Month m: Month.values()) { DayOfWeek days = LocalDate.now().withYear(2019).with(m).with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek(); }
import java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; import java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] argv) { List<DayOfWeek>dayOfWeek = new ArrayList<>(); for (Month m: Month.values()) { DayOfWeek days = LocalDate.now().withYear(2019).with(m).with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek(); dayOfWeek.add(days); } System.out.println("Last day of each month in 2019 = "+dayOfWeek); } }
输出结果
Last day of each month in 2019 = [THURSDAY, THURSDAY, SUNDAY, TUESDAY, FRIDAY, SUNDAY, WEDNESDAY, SATURDAY, MONDAY, THURSDAY, SATURDAY, TUESDAY]