美文网首页
java获取中国节假日

java获取中国节假日

作者: 归来_仍是少年 | 来源:发表于2021-03-10 18:54 被阅读0次

    引入fastjson和hutool

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.75</version>
    </dependency>
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.3.0</version>
    </dependency>
    

    返回1代表正常休息日,2代表国家法定节假日

    import cn.hutool.core.date.DateUtil;
    import cn.hutool.http.HttpRequest;
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.TypeReference;
    
    import java.util.HashMap;
    import java.util.TreeMap;
    
    public class TestHoliday {
        public static void main(String[] args) {
            String yearMonth = DateUtil.format(DateUtil.date(), "yyyyMM");
    //        String yearMonth = "202105";
            String url = "https://zhiwei-tech.com/console/api/holiday/" + yearMonth;
            String body = HttpRequest.get(url).setEncodeUrlParams(true).execute().body();
            HashMap<String, String> yearMonthMap =
                    JSON.parseObject(body, new TypeReference<HashMap<String, String>>() {
                    });
            String monthHoliday = yearMonthMap.get(yearMonth);
            TreeMap<Integer, Integer> treeMap =
                    JSON.parseObject(monthHoliday, new TypeReference<TreeMap<Integer, Integer>>() {
                    });
            for (Integer key : treeMap.keySet()) {
                System.out.println(key + "----------" + treeMap.get(key));
            }
        }
    }
    

    结果

    1----------2
    2----------2
    3----------2
    4----------2
    5----------2
    9----------1
    15----------1
    16----------1
    22----------1
    23----------1
    29----------1
    30----------1
    

    相关文章

      网友评论

          本文标题:java获取中国节假日

          本文链接:https://www.haomeiwen.com/subject/cjjwqltx.html