Java教程

java判断今天是否为工作日

本文主要是介绍java判断今天是否为工作日,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

该方法的实现是通过调用一个网上的大佬的线上API(http://tool.bitefu.net/jiari/)实现的,所以该方法的实现需要联网才可以。  

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

public class HolidayUtil {
    /**
     * 判断今日是否为工作日
     * @param httpArg
     * @return
     */
    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 String judgeDate() {
        //获取当前时间,格式为yyyyMMdd
        String woringDays = DateGestalt.formatDate(Calendar.getInstance(), "yyyyMMdd");
        String jsonResult = HolidayUtil.request(woringDays);
        // 0 上班  1周末 2节假日
        if ("0".equals(jsonResult)) {
            woringDays = "0";
        }
        if ("1".equals(jsonResult)) {
            woringDays = "1";

        }
        if ("2".equals(jsonResult)) {
            woringDays = "2";
        }
       return woringDays;
    }
}

 

这篇关于java判断今天是否为工作日的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!