package com.logicbig.example.yearmonth;
import java.time.YearMonth;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQuery;
import java.time.temporal.UnsupportedTemporalTypeException;
public class QueryExample2 {
public static void main(String... args) {
YearMonth y = YearMonth.of(1940, 5);
System.out.println(y);
Boolean b = y.query(secondWorldWarPeriodQuery);
System.out.println(b);
YearMonth y2 = YearMonth.now();
System.out.println(y2);
Boolean b2 = y2.query(secondWorldWarPeriodQuery);
System.out.println(b2);
}
private static final TemporalQuery<Boolean> secondWorldWarPeriodQuery = temporal -> {
if (temporal.isSupported(ChronoField.YEAR) &&
temporal.isSupported(ChronoField.MONTH_OF_YEAR)) {
YearMonth y = YearMonth.from(temporal);
return y.isAfter(YearMonth.of(1939, 9)) && y.isBefore(YearMonth.of(1945, 9));
}
throw new UnsupportedTemporalTypeException("The temporal must support year and year-of-month fields");
};
}