欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

判断某一天是否是节假日

程序员文章站 2022-05-16 18:50:28
...

请求url

http://www.easybots.cn/api/holiday.php?d=20160104

返回值:

工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2;

package xiaoshuang.spark;

import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import sun.lwawt.macosx.CPrinterDevice;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

public class APIHelper {
    /* 工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2;*/
    private static final String DATE_API = "http://www.easybots.cn/api/holiday.php";

    /**
     * 判断所给日期为什么类型,
     *
     * @param date 日期 20200404
     * @return 0 工作日, 1 休息日, 2 节假日, -1 为判断出错
     */
    public static int holidayType(String dateStr) {
        Integer result = -1;
        String httpArg = "d=" + dateStr;
        String jsonResult = request(DATE_API, httpArg);
        System.out.println(jsonResult);
        result = Integer.parseInt(jsonResult.replace("\"", "")
                .replace("{", "")
                .replace("}", "")
                .split(":")[1]);
        return result;
    }

    public static Map<String, String> getMoreDaysType(int days) {
        String dateStr = "";
        LocalDate localDate = LocalDate.now();
        String pt = String.valueOf(localDate.minusDays(1)).replace("-", "");
        String datept0 = String.valueOf(localDate.minusDays(days)).replace("-", "");
        for (int i = days; i >= 1; i--) {
            String currdays = String.valueOf(localDate.minusDays(i)).replace("-", "");
            if (i == 1) {
                dateStr = dateStr + currdays;
            } else {
                dateStr = dateStr + currdays + ",";
            }
        }
        System.out.println(dateStr);
        String httpArg = "d=" + dateStr;
        String jsonResult = request(DATE_API, httpArg);
        System.out.println(jsonResult);
        Map<String, String> result = new LinkedHashMap<>();
        String[] arr = jsonResult.replace("\"", "")
                .replace("{", "")
                .replace("}", "")
                .split(",");
        for (String str : arr) {
            String[] elem = str.split(":");
            result.put(elem[0], elem[1]);
        }
        return result;
    }

    /**
     * @param httpUrl :请求接口
     * @param httpArg :参数
     * @return 返回结果
     */
    public static String request(String httpUrl, String httpArg) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?" + httpArg;

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
            }
            reader.close();
            result = sbf.toString();
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public static void main(String[] args) throws ParseException {
//        System.out.println(holidayType("20200404"));
        System.out.println(getMoreDaysType(30));


    }
}

 20200229,20200301,20200302,20200303,20200304,20200305,20200306,20200307,20200308,20200309,20200310,20200311,20200312,20200313,20200314,20200315,20200316,20200317,20200318,20200319,20200320,20200321,20200322,20200323,20200324,20200325,20200326,20200327,20200328,20200329


{"20200229":"1","20200301":"1","20200302":"0","20200303":"0","20200304":"0","20200305":"0","20200306":"0","20200307":"1","20200308":"1","20200309":"0","20200310":"0","20200311":"0","20200312":"0","20200313":"0","20200314":"1","20200315":"1","20200316":"0","20200317":"0","20200318":"0","20200319":"0","20200320":"0","20200321":"1","20200322":"1","20200323":"0","20200324":"0","20200325":"0","20200326":"0","20200327":"0","20200328":"1","20200329":"1"}


{20200229=1, 20200301=1, 20200302=0, 20200303=0, 20200304=0, 20200305=0, 20200306=0, 20200307=1, 20200308=1, 20200309=0, 20200310=0, 20200311=0, 20200312=0, 20200313=0, 20200314=1, 20200315=1, 20200316=0, 20200317=0, 20200318=0, 20200319=0, 20200320=0, 20200321=1, 20200322=1, 20200323=0, 20200324=0, 20200325=0, 20200326=0, 20200327=0, 20200328=1, 20200329=1}

 

 

相关标签: 小工具