Java - Query if a Date time is Friday 13th

Description

Query if a Date time is Friday 13th

Demo

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 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;
      }//from ww w .  j  a  va  2  s . c o m
    }
    return Boolean.FALSE;
  }

  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(2014, 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);

  }
}

Result

Related Topic