集册 Java实例教程 一个实现临时查询接口的类

一个实现临时查询接口的类

欢马劈雪     最近更新时间:2020-01-02 10:19:05

578
一个实现临时查询接口的类

import static java.time.DayOfWeek.FRIDAY;

import static java.time.temporal.ChronoField.DAY_OF_MONTH;/*from 时 代 J a v a - N o w J a v a . c o m*/

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 N o w  J a v a  .   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) {

    
展开阅读全文