Work Note 6 - java call interface to get work/holiday/rest day

Since it is necessary to distinguish working days, rest days, and holidays when doing functions today

So I wrote a tool class. I searched on the Internet and Baidu api has been taken off the shelf, so I found another address, which is very useful.

Paste the code directly

package com.sctele.core.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util. *;

/**
 * Judge weekdays/weekends/holidays
 */
public class HolidayUtils {

    /**
     * @param httpArg : parameter
     * @return return result
     */
    public static String request(String httpArg) {
        String httpUrl = "http://tool.bitefu.net/jiari/";
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?d=" + 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();
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return result;
    }


    public static List<Date> findDates(Date dBegin, Date dEnd) {
        List lDate = new ArrayList();
        lDate.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        // Set the time of this Calendar with the given Date
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // Set the time of this Calendar with the given Date
        calEnd.setTime(dEnd);
        // Test if this date is after the specified date
        while (dEnd.after(calBegin.getTime())) {
            // Add or subtract the specified amount of time for the given calendar field according to the calendar's rules
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }


//    public static void main(String[] args) {
// //Determine whether today is a weekday weekend or a holiday
// //0 work 1 weekend and 2 holidays
//        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
//        String httpArg = f.format(new Date());
//        System.out.println(httpArg);
//        String jsonResult = request(httpArg);
//        System.out.println(jsonResult);
//    }

    public static void main(String[] args) throws Exception {
        // Determine the situation of a time period
        String start = "2018-01-01";
        String end = "2018-03-31";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Date dBegin = sdf.parse(start);
        Date dEnd = sdf.parse(end);
        List<Date> lDate = findDates(dBegin, dEnd);
        StringBuffer dataStr = new StringBuffer();
        for (Date date : lDate) {
            //System.out.println(sdf.format(date));
            dataStr.append(dateFormat.format(date)).append(",");
        }
        String s = dataStr.substring(0,dataStr.length()-1).toString();
        String jsonResult = request(s);
        System.out.println(jsonResult);
    }

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326047857&siteId=291194637