Java examples for Date Time:Date Time Query
A Class Implementing the TemporalQuery Interface
import static java.time.DayOfWeek.FRIDAY; 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; import java.time.temporal.TemporalQuery; class Friday13Query implements TemporalQuery<Boolean> { public final static Friday13Query IS_FRIDAY_13 = new Friday13Query(); // No objects can be created private Friday13Query() { }/*from w w w . j av a 2s . c o m*/ @Override public Boolean queryFrom(TemporalAccessor temporal) { if (temporal.isSupported(DAY_OF_MONTH) && temporal.isSupported(DAY_OF_WEEK)) { int dayOfMonth = temporal.get(DAY_OF_MONTH); int weekDay = temporal.get(DAY_OF_WEEK); DayOfWeek dayOfWeek = DayOfWeek.of(weekDay); if (dayOfMonth == 13 && dayOfWeek == FRIDAY) { return Boolean.TRUE; } } return Boolean.FALSE; } } public class Main { public static void main(String[] args) { LocalDate ld1 = LocalDate.of(2013, 12, 13); Boolean isFriday13 = ld1.query(Friday13Query.IS_FRIDAY_13); System.out.println("Date: " + ld1 + ", isFriday13: " + isFriday13); LocalDate ld2 = LocalDate.of(2017, 1, 10); isFriday13 = ld2.query(Friday13Query.IS_FRIDAY_13); System.out.println("Date: " + ld2 + ", isFriday13: " + isFriday13); LocalTime lt = LocalTime.of(7, 30, 45); isFriday13 = lt.query(Friday13Query.IS_FRIDAY_13); 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); int weekDay = temporal.get(DAY_OF_WEEK); DayOfWeek dayOfWeek = DayOfWeek.of(weekDay); if (dayOfMonth == 13 && dayOfWeek == FRIDAY) { return Boolean.TRUE; } } return Boolean.FALSE; } }