提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用查询方法在LocalDate上查询日期
import static java.time.DayOfWeek.FRIDAY;//来 自 nowjava - 时 代 Java import static java.time.temporal.ChronoField.DAY_OF_MONTH; import static java.time.temporal.ChronoField.DAY_OF_WEEK; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalTime; import java.time.temporal.TemporalAccessor; public class Main { public static void main(String[] args) { LocalDate ld1 = LocalDate.of(2013, 12, 13); Boolean isFriday13 = ld1.query(Main::isFriday13); System.out.println("Date: " + ld1 + ", isFriday13: " + isFriday13); LocalDate ld2 = LocalDate.of(2017, 1, 10); isFriday13 = ld2.query(Main::isFriday13); System.out.println("Date: " + ld2 + ", isFriday13: " + isFriday13); /** from * 时代Java公众号 - nowjava.com **/ LocalTime lt = LocalTime.of(7, 30, 45); isFriday13 = lt.query(Main::isFriday13); System.out.println("Time: " + lt + ", isFriday13: " + isFriday13); } public static Boolean isFriday13(TemporalAccessor temporal) { if (temporal.isSupported(DAY_OF_MONTH) && temporal.isSupported(DAY_OF_WEEK)) { int dayOfMonth = temporal.get(DAY_OF_MONTH);