万年历农历法定节假日数据查询工具

1.数据来源于百度搜索置顶日历:

在这里插入图片描述

2.代码:

http调用及数据处理均采用了hutool, 也可以用别的工具。
hutool 依赖如下:

<dependency>
  	<groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>4.6.2</version>
</dependency>
package com.visy.utils;

import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author visy.wang
 * @description: 日历数据查询工具
 * @date 2023/1/29 15:55
 */
@Slf4j
public class CalendarUtils {

    public static void main(String[] args) {
        JSONArray list = queryCalendarList(2023, 2);
        //打印
        if(list != null){
        	//只打印一行看看
        	System.out.println(JSONUtil.formatJsonStr(list.get(0).toString()));
		}
    }

    /**
     * 查询日历列表,注意:返回结果的oDate采用的时区是0时区
     * @param year 年,如:2023
     * @param month 月,如:2
     * @return 返回当前月,上一个月以及下一个月的日历数据
     */
    public static JSONArray queryCalendarList(int year, int month){
        String url = "https://opendata.baidu.com/api.php";

        //拼接参数
        url = url + "?" + getQueryStr(year, month);

        log.info("完整请求地址: {}", url);

        //发起调用
        String response = HttpUtil.get(url);

        //可以看到返回的是JSONP
        log.info("原始响应结果: {}", response);

        //去除多余字符,提取JSON部分(返回结果为JSONP时)
        String jsonStr = response.substring(response.indexOf("{"), response.lastIndexOf("}")+1);
        log.info("JSON响应体: {}", jsonStr);

        //解析返回结果
        JSONObject json = JSONUtil.parseObj(jsonStr);

        //仅当状态=0时调用成功
        if(!"0".equals(json.getStr("status"))){
            log.info("接口调用失败");
            return null;
        }

        //提取data数组
        JSONArray data = json.getJSONArray("data");
        if(data.isEmpty()){
            return null;
        }

        //取第一个元素
        JSONObject data_0 = data.getJSONObject(0);

        //提取almanac数组并返回
        return data_0.getJSONArray("almanac");
    }

    private static String getQueryStr(int year, int month){
        Map<String,Object> params = new HashMap<>();
        params.put("tn", "wisetpl");
        params.put("format", "json");
        params.put("resource_id", "39043");
        params.put("t", System.currentTimeMillis());
        params.put("cb", "callback"); //不加这个参数返回结果为JSON,加了返回的是JSONP
        params.put("query", year+"年"+month+"月");

        StringBuffer qs = new StringBuffer();
        params.forEach((k, v) -> {
            qs.append(k).append("=").append(v).append("&");
        });

        return URLUtil.encode(qs.deleteCharAt(qs.length()-1).toString(), StandardCharsets.UTF_8);
    }
}
3.输出
完整请求地址: https://opendata.baidu.com/api.php?t=1674981971139&query=2023%E5%B9%B42%E6%9C%88&format=json&resource_id=39043&tn=wisetpl&cb=callback
原始响应结果: /**/callback({"status":"0","t":"1674981971139","set_cache_time":"","data":[...]});
{
    "oDate":"2022-12-31T16:00:00.000Z",
    "year":"2023",
    "lunarDate":"10",
    "yj_from":"51wnl",
    "isBigMonth":"1",
    "lDate":"初十",
    "lMonth":"腊",
    "type":"h",
    "gzMonth":"壬子",
    "lunarMonth":"12",
    "gzYear":"壬寅",
    "month":"1",
    "cnDay":"日",
    "animal":"虎",
    "term":"元旦",
    "avoid":"栽种.安门.治病.作灶",
    "gzDate":"己未",
    "suit":"动土.祈福.安床.架马.开厕.祭祀.入殓.成人礼.成服.除服.伐木.结网.开池.求子",
    "day":"1",
    "lunarYear":"2022",
    "status":"1"
}
4.字段说明:
字段名描述例子
oDate时间(0时区)2022-12-31T16:00:00.000Z
year年(阳历)2023
month月(阳历)1
day日(阳历)1
lunarYear年(农历)2022
lunarMonth月(农历)12
lunarDate日(农历)10
lMonth月(农历描述)
lDate日(农历描述)初十
gzYear年-干支壬寅
gzMonth月-干支壬子
gzDate日-干支己未
animal生肖年
cnDay星期描述(一二三四五六日)
isBigMonth是否是农历大月(1-是)1
term节假日描述(含节气)元旦
status节假日状态(1-休假,2-补班)1
type节假日类型h
yj_from数据来源51wnl
suit动土.祈福.安床.架马.开厕.祭祀.入殓.成人礼.成服.除服.伐木.结网.开池.求子
avoid栽种.安门.治病.作灶
5.附新地址

https://opendata.baidu.com/data/inner?tn=reserved_all_res_tn&type=json&resource_id=52109&apiType=yearMonthData&query=2023年12月

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
以下是C语言实现万年历农历的代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <windows.h> // 定义全局变量 int year, month, day; int week, days; int lunar_year, lunar_month, lunar_day; char lunar_month_name[10]; // 定义函数 void gotoxy(int x, int y); void get_date(); void print_calendar(); void print_lunar_calendar(); void get_lunar_date(); void get_lunar_month_name(); void month(int y, int m); int get_lunar_month_days(int year, int month); int get_lunar_year_days(int year); int get_lunar_month(int year, int day); int get_lunar_day(int year, int month, int day); int get_lunar_year(int year); // 主函数 int main() { printf("请输入年月:"); scanf("%d%d", &year, &month); month(year, month); return 0; } // 移动光标 void gotoxy(int x, int y) { COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } // 获取公历日期 void get_date() { SYSTEMTIME sys; GetLocalTime(&sys); year = sys.wYear; month = sys.wMonth; day = sys.wDay; } // 打印公历日历 void print_calendar() { printf("日 一 二 三 四 五 六\n"); for (int i = 1; i < week; i++) { printf(" "); } for (int i = 1; i <= days; i++) { if (week == 7) { printf("%2d\n", i); week = 0; } else { printf("%2d ", i); } week++; } printf("\n"); } // 打印农历日历 void print_lunar_calendar() { printf("农历:%s月%s\n", lunar_month_name, lunar_day == 1 ? "初一" : lunar_day == 10 ? "初十" : lunar_day == 20 ? "二十" : lunar_day == 30 ? "三十" : ""); } // 获取农历日期 void get_lunar_date() { int i, leap = 0, temp = 0; for (i = 1900; i <= year && temp <= 0x10000; i++) { temp = get_lunar_year_days(i); } if (year == i - 1) { temp = get_lunar_year_days(year); } days = temp - 1; for (i = 1; i <= 12 && days > 0; i++) { if (get_lunar_month_days(year, i) == 13) { leap = leap_month(year); } else { leap = 0; } if (leap == i && days > 0) { i--; days -= get_lunar_month_days(year, leap); leap = 0; } else { days -= get_lunar_month_days(year, i); } } if (days == 0 && leap > 0 && i == leap + 1) { if (leap == 12) { leap = 1; } else { leap++; } } if (days < 0) { days += get_lunar_month_days(year, --i); } lunar_month = i; lunar_day = days + 1; } // 获取农历月份名称 void get_lunar_month_name() { char *month_name[] = {"", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"}; strcpy(lunar_month_name, month_name[lunar_month]); } // 获取农历年份的总天数 int get_lunar_year_days(int year) { int i, sum = 348; for (i = 0x8000; i > 0x8; i >>= 1) { sum += (get_lunar_year(year) & i) ? 1 : 0; } return sum + leap_month(year); } // 获取农历月份的总天数 int get_lunar_month_days(int year, int month) { return (get_lunar_year(year) & (0x10000 >> month)) ? 30 : 29; } // 获取农历年份的闰月月份 int leap_month(int year) { return get_lunar_year(year) & 0xf; } // 获取农历年份的二进制码 int get_lunar_year(int year) { int i, sum = 0; for (i = 0x8000; i > 0x8; i >>= 1) { sum += (get_lunar_year(year) & i) ? 1 : 0; } return sum; } // 获取农历月份 int get_lunar_month(int year, int day) { int i, leap = 0, temp = 0; for (i = 1; i <= 12 && day > 0; i++) { if (get_lunar_month_days(year, i) == 13) { leap = leap_month(year); } else { leap = 0; } if (leap > 0 && i == leap + 1 && day > 0) { --i; day -= get_lunar_month_days(year, leap); leap = 0; } else { day -= get_lunar_month_days(year, i); } } if (day == 0 && leap > 0 && i == leap + 1) { if (leap == 12) { leap = 1; } else { leap++; } } if (day < 0) { day += get_lunar_month_days(year, --i); } return i; } // 获取农历日 int get_lunar_day(int year, int month, int day) { int i, leap = 0, temp = 0; for (i = 1; i < month; i++) { if (get_lunar_month_days(year, i) == 13) { leap = leap_month(year); } else { leap = 0; } if (leap > 0 && i == leap + 1) { --i; leap = 0; } } return day; } // 获取农历年份 int get_lunar_year(int year) { int i, leap = 0, temp = 0; for (i = 1; i <= 12 && temp < year; i++) { temp += get_lunar_month_days(year, i); if (get_lunar_month_days(year, i) == 13) { leap = leap_month(year); } else { leap = 0; } if (leap > 0 && i == leap + 1) { --i; leap = 0; } } if (temp > year) { --i; } return i; } // 打印一个月的日历 void month(int y, int m) { int i, j; get_date(); week = 0; days = 0; lunar_year = 0; lunar_month = 0; lunar_day = 0; get_lunar_date(); get_lunar_month_name(); printf("%d年%d月\n", y, m); printf("公历:\n"); printf("日 一 二 三 四 五 六\n"); for (i = 1; i < week; i++) { printf(" "); } for (i = 1; i <= days; i++) { if (week == 7) { printf("%2d\n", i); week = 0; } else { printf("%2d ", i); } week++; } printf("\n"); printf("农历:\n"); printf("日 一 二 三 四 五 六\n"); for (i = 1; i < week; i++) { printf(" "); } for (i = 1; i <= days; i++) { if (week == 7) { print_lunar_calendar(); printf("\n"); week = 0; } else { print_lunar_calendar(); } week++; } printf("\n"); } // 相关问题:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值