Java TemporalQuery implement to create custom query

Description

Java TemporalQuery implement to create custom query


import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalQuery;

class FridayThirteenQuery implements TemporalQuery<Boolean> {
  // Returns TRUE if the date occurs on Friday the 13th.
  public Boolean queryFrom(TemporalAccessor date) {
    return ((date.get(ChronoField.DAY_OF_MONTH) == 13) && 
         (date.get(ChronoField.DAY_OF_WEEK) == 5));
  }//  w ww .  j  a va2s. co m
}

public class Main {

  public static void main(String[] args) {
    Month month = Month.valueOf("MAY");
    int day = 13;
    LocalDate date  = Year.now().atMonth(month).atDay(day);
    System.out.println(date.query(new FridayThirteenQuery()));
  }
}



PreviousNext

Related