集册 Java实例教程 Java查询日期发生在13号星期五

Java查询日期发生在13号星期五

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

409
Java查询日期发生在13号星期五

import java.time.Month;

import java.time.Year;
/** 
来 自 
N o w  J a v a  . c o m
**/

import java.time.LocalDate;

import java.time.DayOfWeek;

import java.time.DateTimeException;


import java.time.temporal.TemporalQuery;

import java.time.temporal.TemporalAccessor;


import java.io.PrintStream;

import java.time.Month;

import java.time.Year;

import java.time.LocalDate;

import java.time.DateTimeException;


import java.time.temporal.TemporalQuery;/*时   代     Java  公  众  号 - nowjava.com 提供*/

import java.time.temporal.TemporalAccessor;

import java.time.temporal.ChronoField;


import java.io.PrintStream;

import java.lang.Boolean;


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));

    }

}

public class Superstitious {


    public static void main(String[] args) {

        Month month = null;

        LocalDate date = null;


        if (args.length < 2) {

            System.out.printf("Usage: Superstitious <month> <day>%n");

            throw new IllegalArgumentException();

        }


        try {

            month = Month.valueOf(args[0].toUpperCase());

        } catch (IllegalArgumentException exc) {

            System.out.printf("%s is not a valid month.%n", args[0]);

            throw exc; // Rethrow the exception.

        }


        int day = Integer.parseInt(args[1]);


        try {

            date = Year.now().atMonth(month).atDay(day);

        } catch (DateTimeException exc) {

            System.out.printf("%s %s is not a valid date.%n", month, day);

            throw exc; // Rethrow the exception.

        }


        System.out.println(date.query(new FridayThirteenQuery()));

    }

}


展开阅读全文