Query date with query method on LocalDate - Java Date Time

Java examples for Date Time:Date Time Query

Description

Query date with query method on LocalDate

Demo Code

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;

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);

    LocalTime lt = LocalTime.of(7, 30, 45);
    isFriday13 = lt.query(Main::isFriday13);
    System.out.println("Time: " + lt + ", isFriday13: " + isFriday13);
  }//from  w ww .  jav  a 2  s. c  om

  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;
  }
}

Related Tutorials