做一个java程序判断节假日

比如传一个2020-6-5 10:30

1.判断是否是工作日

2.判读是白天还是晚上

3.判断是哪个季节

这个就很简单啊。

public static void main(String[] args) {
        String str = "2020-06-05 10:30:00";
        LocalDateTime parse = LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        int week = parse.getDayOfWeek().getValue();

        if (week >= 1 && week <= 5) System.out.println("工作日");
        else System.out.println("周末");

        int hour = parse.getHour();
        if (hour >= 7 && hour < 19) { // 5点天应该是白天吧
            System.out.println("当前时间为白天");
        } else {
            System.out.println("当前时间为晚上");
        }
        int monthValue = parse.getMonthValue();
        if (monthValue == 3 || monthValue == 4 || monthValue == 5) System.out.println("春季");
        else if (monthValue == 6 || monthValue == 7 || monthValue == 8) System.out.println("夏季");
        else if (monthValue == 9 || monthValue == 10 || monthValue == 11) System.out.println("秋季");
        else if (monthValue == 12 || monthValue == 1 || monthValue == 2) System.out.println("冬季");
    }

 

如果工作日指的是固定周一到周五,直接判断就好

如果包括节假日,建议调用第三方接口,不然国家节假日修改时,你都要自己修改很麻烦