Java中可以通过调用第三方API或者自己实现一个判断是否是法定节假日的方法。

  1. 调用第三方API

可以使用国内外许多第三方API来获取节假日信息,例如中国节假日API(http://timor.tech/api/holiday/)等。

使用该API,可以通过发送GET请求,获取某一天是否为节假日。例如:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HolidayChecker {
    public static void main(String[] args) throws IOException {
        String date = "20220101";
        String url = "http://timor.tech/api/holiday/info/" + date;
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        int responseCode = con.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            System.out.println(response.toString());
        } else {
            System.out.println("GET request not worked");
        }
    }
}
  1. 自己实现一个判断是否是法定节假日的方法

可以根据国家法定节假日的规定,自己实现一个判断是否是法定节假日的方法。例如:

import java.time.LocalDate;

public class HolidayChecker {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2022, 1, 1);
        if (isHoliday(date)) {
            System.out.println("是法定节假日");
        } else {
            System.out.println("不是法定节假日");
        }
    }

    public static boolean isHoliday(LocalDate date) {
        int year = date.getYear();
        int month = date.getMonthValue();
        int day = date.getDayOfMonth();
        // 判断元旦节
        if (month == 1 && day == 1) {
            return true;
        }
        // 判断春节
        if (isSpringFestival(year, month, day)) {
            return true;
        }
        // 判断清明节
        if (isQingmingFestival(year, month, day)) {
            return true;
        }
        // 判断劳动节
        if (month == 5 && (day == 1 || day == 2 || day == 3)) {
            return true;
        }
        // 判断端午节
        if (isDragonBoatFestival(year, month, day)) {
            return true;
        }
        // 判断中秋节
        if (isMidAutumnFestival(year, month, day)) {
            return true;
        }
        // 判断国庆节
        if (month == 10 && (day == 1 || day == 2 || day == 3 || day == 4 || day == 5 || day == 6 || day == 7)) {
            return true;
        }
        return false;
    }

    private static boolean isSpringFestival(int year, int month, int day) {
        if (month == 2) {
            if (year == 2022 && day >= 1 && day <= 8) {
                return true;
            }
            // 根据农历计算春节日期
            int[] lunarMonthDays = LunarCalendar.getLunarMonthDays(year);
            int[] lunarMonthLeapDays = LunarCalendar.getLunarMonthLeapDays(year);
            int lunarMonth = 1;
            int lunarDay = 1;
            boolean isLeapYear = LunarCalendar.isLeapYear(year);
            int i = 0;
            while (day > 0) {
                if (i == 0 && isLeapYear) {
                    // 如果是闰年,第一个月是29天
                    lunarDay += 29;
                } else {
                    lunarDay += lunarMonthDays[i];
                }
                if (day > lunarDay) {
                    lunarMonth++;
                }
                day -= lunarDay;
                lunarDay = 0;
                i++;
            }
            if (lunarMonth == 1 && lunarDay == 1) {
                return true;
            }
        }
        return false;
    }

    private static boolean isQingmingFestival(int year, int month, int day) {
        if (month == 4) {
            if (year == 2022 && day == 5) {
                return true;
            }
            // 根据农历计算清明节日期
            int[] lunarMonthDays = LunarCalendar.getLunarMonthDays(year);
            int[] lunarMonthLeapDays = LunarCalendar.getLunarMonthLeapDays(year);
            int lunarMonth = 1;
            int lunarDay = 1;
            boolean isLeapYear = LunarCalendar.isLeapYear(year);
            int i = 0;
            while (day > 0) {
                if (i == 2 && isLeapYear) {
                    // 如果是闰年,第3个月是30天
                    lunarDay += 30;
                } else {
                    lunarDay += lunarMonthDays[i];
                }
                if (day > lunarDay) {
                    lunarMonth++;
                }
                day -= lunarDay;
                lunarDay = 0;
                i++;
            }
            if (lunarMonth == 5 && lunarDay == 1) {
                return true;
            }
        }
        return false;
    }

    private static boolean isDragonBoatFestival(int year, int month, int day) {
        if (month == 6) {
            // 根据农历计算端午节日期
            int[] lunarMonthDays = LunarCalendar.getLunarMonthDays(year);
            int[] lunarMonthLeapDays = LunarCalendar.getLunarMonthLeapDays(year);
            int lunarMonth = 1;
            int lunarDay = 1;
            boolean isLeapYear = LunarCalendar.isLeapYear(year);
            int i = 0;
            while (day > 0) {
                if (i == 4 && isLeapYear) {
                    // 如果是闰年,第5个月是30天
                    lunarDay += 30;
                } else {
                    lunarDay += lunarMonthDays[i];
                }
                if (day > lunarDay) {
                    lunarMonth++;
                }
                day -= lunarDay;
                lunarDay = 0;
                i++;
            }
            if (lunarMonth == 5 && lunarDay == 5) {
                return true;
            }
        }
        return false;
    }

    private static boolean isMidAutumnFestival(int year, int month, int day) {
        if (month == 9) {
            if (year == 2022 && day == 10) {
                return true;
            }
            // 根据农历计算中秋节日期
            int[] lunarMonthDays = LunarCalendar.getLunarMonthDays(year);
            int[] lunarMonthLeapDays = LunarCalendar.getLunarMonthLeapDays(year);
            int lunarMonth = 1;
            int lunarDay = 1;
            boolean isLeapYear = LunarCalendar.isLeapYear(year);
            int i = 0;
            while (day > 0) {
                if (i == 7 && isLeapYear) {
                    // 如果是闰年,第8个月是30天
                    lunarDay += 30;
                } else {
                    lunarDay += lunarMonthDays[i];
                }
                if (day > lunarDay) {
                    lunarMonth++;
                }
                day -= lunarDay;
                lunarDay = 0;
                i++;
            }
            if (lunarMonth == 8 && lunarDay == 15) {
                return true;
            }
        }
        return false;
    }
}

需要注意的是,法定节假日的计算需要根据公历和农历两种日期进行计算,而且不同年份的法定节假日日期也可能不同,因此需要进行详细的计算

java判断是否是法定节假日

原文地址: http://www.cveoy.top/t/topic/eCFX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录