Java实现是否为节假日、工作日判断(调用“http://api.goseek.cn/Tools/holiday”接口)

时间:2024-02-29 16:44:47

工具类:

package com.ai.rai.group.system;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.collections.MapUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * @version 1.0
 * @ClassName HolidayCopy
 * @Description
 * @Author 74981
 * @Date 2018/9/27 14:30
 */
public class HolidayCopy {
    /**
     * @Description: 入参格式为“yyyyMMdd”如“20181001”
     * @param
     * @return result(0 工作日 1周末 2节假日)
     * @throws
     * @author liushiwei
     * @date 2018/9/27 15:34
     */
    public static String request(String httpArg) {
        //工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2
        String httpUrl = "http://api.goseek.cn/Tools/holiday";
        String result = "";
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?date=" + httpArg;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = MapUtils.getString(jsonStringToMap(String.valueOf(sbf)),"data");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @Description: JSONString转Map
     * @param 
     * @return 
     * @throws
     * @author liushiwei
     * @date 2018/9/27 15:42 
     */
    private static Map<String, Object> jsonStringToMap(String json) {
        //String转成JSONObject形式
        JSONObject jsonArray = JSONObject.parseObject(json);
        Map<String, Object> param = JSONObject.parseObject(jsonArray.toJSONString(), new TypeReference<Map<String, Object>>() {});
        return param;
    }

}

测试类:

package com.ai.rai.group.system;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @version 1.0
 * @ClassName MainTest
 * @Description
 * @Author 74981
 * @Date 2018/9/27 15:52
 */
public class MainTest {

    public static void main(String[] args) {
        HolidayCopy holidayCopy = new HolidayCopy();
        //判断今天是否是工作日 周末 还是节假日
        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
        String httpArg = "20180924";
        String jsonResult = holidayCopy.request(httpArg);
        System.out.println(httpArg+" =中秋节(放假)礼拜一= "+jsonResult);

        httpArg = "20180916";
        jsonResult = holidayCopy.request(httpArg);
        System.out.println(httpArg+" =正常周末(休息)礼拜天 = "+jsonResult);

        httpArg = "20180925";
        jsonResult = holidayCopy.request(httpArg);
        System.out.println(httpArg+" =工作日(上班)礼拜二 = "+jsonResult);

        httpArg = "20180930";
        jsonResult = holidayCopy.request(httpArg);
        System.out.println(httpArg+" =工作日(补班)礼拜天 = "+jsonResult);


        httpArg = f.format(new Date());
        jsonResult = holidayCopy.request(httpArg);
        System.out.println(httpArg+" =随缘(XXX)今天 = "+jsonResult);
    }
}

控制台:

 

用法举例:
检查一个日期是否为节假日 ?date=20180925

多个日期查询和按照年份、月份查询???

示例: 
http://api.goseek.cn/Tools/holiday?date=20180925
返回数据: {"code":10001,"data":2} 

参考:https://blog.csdn.net/rickiyeat/article/details/52512355