免费的不少,小项目使用较多,存在时间较久的,推荐 https://timor.tech/api/holiday

案例 获取某个月的休息日=节假日+周六+周日

public class UnirestDemo {
    public static void main(String[] args) {
        String url="http://timor.tech/api/holiday/year/2025-2?type=N&week=Y";
        HttpResponse<JsonNode> response = Unirest.get(url)
                .header("Accept", "application/json")
                .header("Content-Type", "application/json")
                .asJson();
        // 检查HTTP状态码
        if (response.getStatus() == 200) {
            JsonNode jsonResponse = response.getBody();
            JSONObject jsonObject = jsonResponse.getObject();

            // 检查API返回码
            if (jsonObject.getInt("code") == 0) {
                System.out.println("API调用成功!");
                // 获取holiday对象
                JSONObject holidays = jsonObject.getJSONObject("holiday");

                System.out.println("=== 2025年2月节假日信息 ===");

                // 遍历所有日期
                for (String dateKey : holidays.keySet()) {
                    JSONObject holidayInfo = holidays.getJSONObject(dateKey);

                    String date = holidayInfo.getString("date");
                    String name = holidayInfo.getString("name");
                    boolean isHoliday = holidayInfo.getBoolean("holiday");
                    int wage = holidayInfo.getInt("wage");

                    // rest字段可能不存在,需要检查
                    int rest = holidayInfo.has("rest") ? holidayInfo.getInt("rest") : 0;

                    String holidayType = isHoliday ? "节假日" : "补班日";
                    String wageDesc = wage == 2 ? "双倍工资" : "正常工资";

                    System.out.printf("日期: %s, 名称: %s, 类型: %s, 工资: %s",
                            date, name, holidayType, wageDesc);

                    if (rest > 0) {
                        System.out.printf(", 休息: %d天", rest);
                    }

                    System.out.println();
                }
                    int totalHolidays = 0;


                    for (String dateKey : holidays.keySet()) {
                        JSONObject holidayInfo = holidays.getJSONObject(dateKey);

                        if (holidayInfo.getBoolean("holiday")) {
                            totalHolidays++;
                        }
                    }

                    System.out.println("总节假日天数: " + totalHolidays);


            } else {
                System.out.println("API返回错误码: " + jsonObject.getInt("code"));

            }
        } else {
            System.out.println("HTTP请求失败,状态码: " + response.getStatus());

        }

    }

}

代码输出日志

Logo

加入社区!打开量化的大门,首批课程上线啦!

更多推荐