1. import java.time.Month;
    2. import java.time.Year;
    3. import java.time.LocalDate;
    4. import java.time.DateTimeException;
    5. import java.time.temporal.TemporalQuery;
    6. import java.time.temporal.TemporalAccessor;
    7. import java.io.PrintStream;
    8. public class CheckDate {
    9. public static void main(String[] args) {
    10. Month month = null;
    11. LocalDate date = null;
    12. if (args.length < 2)
    13. {
    14. System.out.printf("Usage: CheckDate <month> <day>%n");
    15. throw new IllegalArgumentException();
    16. }
    17. try {
    18. month = Month.valueOf(args[0].toUpperCase());
    19. } catch (IllegalArgumentException exc) {
    20. System.out.printf("%s is not a valid month.%n", args[0]);
    21. throw exc; // Rethrow the exception.
    22. }
    23. int day = Integer.parseInt(args[1]);
    24. try {
    25. date = LocalDate.of(Year.now().getValue(), month, day);
    26. } catch (DateTimeException exc) {
    27. System.out.printf("%s %s is not a valid date.%n", month, day);
    28. throw exc; // Rethrow the exception.
    29. }
    30. // Invoking the query without using a lambda expression.
    31. Boolean isFamilyVacation = date.query(new FamilyVacations());
    32. // Invoking the query using a lambda expression.
    33. Boolean isFamilyBirthday = date.query(FamilyBirthdays::isFamilyBirthday);
    34. if (isFamilyVacation.booleanValue() || isFamilyBirthday.booleanValue())
    35. System.out.printf("%s is an important date!%n", date);
    36. else
    37. System.out.printf("%s is not an important date.%n", date);
    38. }
    39. }
    1. import java.time.Month;
    2. import java.time.Year;
    3. import java.time.LocalDate;
    4. import java.time.DateTimeException;
    5. import java.time.temporal.TemporalQuery;
    6. import java.time.temporal.TemporalAccessor;
    7. import java.time.temporal.ChronoField;
    8. import java.io.PrintStream;
    9. import java.lang.Boolean;
    10. public class FamilyVacations implements TemporalQuery<Boolean> {
    11. // Returns true if the passed-in date occurs during one of the
    12. // family vacations. Because the query compares the month and day only,
    13. // the check succeeds even if the Temporal types are not the same.
    14. // 如果传入日期恰好处于家庭假期的时间段内,返回true
    15. // 因为查询比较的是月份和日份,即使针对是不同类型“期间”,检查也会成功
    16. public Boolean queryFrom(TemporalAccessor date) {
    17. int month = date.get(ChronoField.MONTH_OF_YEAR);
    18. int day = date.get(ChronoField.DAY_OF_MONTH);
    19. // Disneyland over Spring Break
    20. if ((month == Month.APRIL.getValue()) && ((day >= 3) && (day <= 8)))
    21. return Boolean.TRUE;
    22. // Smith family reunion on Lake Saugatuck
    23. if ((month == Month.AUGUST.getValue()) && ((day >= 8) && (day <= 14)))
    24. return Boolean.TRUE;
    25. return Boolean.FALSE;
    26. }
    27. }
    1. import java.time.Month;
    2. import java.time.Year;
    3. import java.time.LocalDate;
    4. import java.time.DateTimeException;
    5. import java.time.temporal.TemporalQuery;
    6. import java.time.temporal.TemporalAccessor;
    7. import java.time.temporal.ChronoField;
    8. import java.io.PrintStream;
    9. import java.lang.Boolean;
    10. public class FamilyBirthdays {
    11. // Returns true if the passed-in date is the same as one of the
    12. // family birthdays. Because the query compares the month and day only,
    13. // the check succeeds even if the Temporal types are not the same.
    14. public static Boolean isFamilyBirthday(TemporalAccessor date) {
    15. int month = date.get(ChronoField.MONTH_OF_YEAR);
    16. int day = date.get(ChronoField.DAY_OF_MONTH);
    17. // Angie's birthday is on April 3.
    18. if ((month == Month.APRIL.getValue()) && (day == 3))
    19. return Boolean.TRUE;
    20. // Sue's birthday is on June 18.
    21. if ((month == Month.JUNE.getValue()) && (day == 18))
    22. return Boolean.TRUE;
    23. // Joe's birthday is on May 29.
    24. if ((month == Month.MAY.getValue()) && (day == 29))
    25. return Boolean.TRUE;
    26. return Boolean.FALSE;
    27. }
    28. }