Java判斷指定日期是否為工作日
轉自:https://www.jianshu.com/p/966659492f2f
轉:https://www.jianshu.com/p/05ccb5783f65
轉:https://www.jianshu.com/p/99931a7a4f0d
獲取指定日期的節假日信息: http://api.goseek.cn/
package com.juqitech.zb.common.util; import net.sf.json.JSONObject; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.text.SimpleDateFormat; /** * 調用API接口判斷日期是否是工作日 周末還是節假日 * 工作日對應結果為 0, 休息日對應結果為 1, 節假日對應的結果為 2 */ public class HolidayUtil { public static int request( String httpArg) { String httpUrl = "http://api.goseek.cn/Tools/holiday"; BufferedReader reader = null; String result = null; StringBuffer sbf = new StringBuffer(); httpUrl = httpUrl + "?date=" + httpArg; int d=0;//工作日對應結果為 0, 休息日對應結果為 1, 節假日對應的結果為 2 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); sbf.append("\r\n"); } reader.close(); result = sbf.toString(); JSONObject ob= JSONObject.fromObject(result); if(ob!=null){ d=Integer.parseInt(ob.getString("data")); } } catch (Exception e) { e.printStackTrace(); } return d; } public static void main(String[] args) { //判斷今天是否是工作日 周末 還是節假日 SimpleDateFormat f=new SimpleDateFormat("yyyyMMdd"); String httpArg="20190216";//f.format(new Date()); System.out.println(httpArg); int n = request(httpArg); System.out.println(n); //工作日對應結果為 0, 休息日對應結果為 1, 節假日對應的結果為 2 } }