java判断节假日工具类


import com.example.schedulelockscreen.model.HolidayInfo;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class HolidayUtils {
    private static final String API_URL = "http://timor.tech/api/holiday/info/";
    private static final HttpClient httpClient = HttpClients.createDefault();
    private static final ObjectMapper objectMapper = new ObjectMapper();
    private static final Map<String, JsonNode> responseCache = new HashMap<>();

    public static boolean isHoliday(String date) throws IOException {
        // 修复点:直接使用 JsonNode 代替 HolidayInfo
        JsonNode response = getApiResponse(date);
        int type = response.path("type").path("type").asInt();
        return type != 0;
    }

    /**
     * 获取API原始响应(修复核心类型问题)
     */
    private static JsonNode getApiResponse(String date) throws IOException {
        if (date == null || date.isEmpty()) {
            date = LocalDate.now().format(DateTimeFormatter.ISO_DATE);
        }
        if (responseCache.containsKey(date)) {
            return responseCache.get(date);
        }

        HttpGet request = new HttpGet(API_URL + date);
        HttpResponse response = httpClient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());

        JsonNode rootNode = objectMapper.readTree(responseBody);
        if (rootNode.path("code").asInt() != 0) {
            throw new RuntimeException("API错误: " + rootNode.path("msg").asText());
        }

        responseCache.put(date, rootNode);
        return rootNode;
    }

    /**
     * 保留的节假日信息解析方法
     */
    public static HolidayInfo getHolidayInfo(String date) throws IOException {
        JsonNode rootNode = getApiResponse(date);
        return parseHolidayInfo(rootNode);
    }
    // 解析API返回的JSON数据
    private static HolidayInfo parseHolidayInfo(JsonNode rootNode) {
        JsonNode typeNode = rootNode.path("type");
        JsonNode holidayNode = rootNode.path("holiday");

        // 处理非节假日情况
        if (holidayNode.isNull()) {
            return new HolidayInfo(
                    typeNode.path("type").asInt(),
                    typeNode.path("name").asText(),
                    typeNode.path("week").asInt(),
                    false, null, 1, false, null
            );
        }

        // 解析节假日数据
        return new HolidayInfo(
                typeNode.path("type").asInt(),
                typeNode.path("name").asText(),
                typeNode.path("week").asInt(),
                holidayNode.path("holiday").asBoolean(),
                holidayNode.path("name").asText(),
                holidayNode.path("wage").asInt(),
                holidayNode.path("after").asBoolean(false),
                holidayNode.path("target").asText(null)
        );
    }

    public static void main(String[] args) throws Exception {
        // 示例1: 判断今日是否节假日
        boolean todayIsHoliday = HolidayUtils.isHoliday("2025-08-24");
        System.out.println("今天是节假日吗? " + todayIsHoliday);

        // 示例2: 获取国庆节完整信息
        HolidayInfo info = HolidayUtils.getHolidayInfo("2025-10-01");
        System.out.println("节日名称: " + info.getHolidayName());
        System.out.println("薪资倍数: " + info.getWageMultiplier() + "倍");
    }
}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐