百度API獲取某個工作日之後的時間
阿新 • • 發佈:2019-02-03
http://apistore.baidu.com/apiworks/servicedetail/1116.html 百度節假日shJSON返回示例 : {"20130101":2,"20130103":2,"20130105":"0","20130201":"0"} 備註 : 功能特點 檢查具體日期是否為節假日,工作日對應結果為 0, 休息日對應結果為 1, 節假日對應的結果為 2; (對應支付工資比例分別為 100%, 200% 和 300%,以方便程式計算 計算方法: 基本工資* (1+結果數值)) 獲取具體月份下的節假日情況,只返回休息日或者節假日資料; 可同時傳遞一個或者多個日期(月份); 支援 2009 年起至2016年 中國法定節假日,以國務院釋出的公告為準,隨時調整及增加;http://www.gov.cn/zfwj/bgtfd.htm或http://www.gov.cn/zhengce/xxgkzl.htm 引數可以以 GET 或 POST 方式傳遞,以 JSON 格式返回結果。 用法舉例 檢查一個日期是否為節假日 ?d=20160101 檢查多個日期是否為節假日 ?d=20160101,20160103,20160105,20161201 獲取2016年10月份節假日 ?d=201610 獲取2016年所有節假日 ?d=2016 獲取2016年1/2月份節假日 ?d=201601,201602
String httpUrl = "http://apis.baidu.com/xiaogg/holiday/holiday"; String httpArg = "d=20151001"; String jsonResult = getHolidays(httpUrl, httpArg); static String apikey = "apikey"; public static String getHolidays(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.setRequestProperty("apikey", apikey); 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(); } catch (Exception e) { e.printStackTrace(); } return result; }
</pre><pre name="code" class="java"> public static Date getEndDay(int x) { Date date; if (x==0){ return new Date(); } String dateStr = "d="; for (int i = 0; i < 14; i++) { date = new Date() + i; dateStr += DateUtil.dateToStr(date) + ","; } String httpUrl = "http://apis.baidu.com/xiaogg/holiday/holiday"; //String httpArg = ""; JsonSlurper jsonSlurper = new JsonSlurper() def json = jsonSlurper.parseText(DateUtil.getHolidays(httpUrl, dateStr)) int count = 0; date = new Date() for (int i = 0; i < json.size(); i++) { if (json.getAt(DateUtil.dateToStr(date)).equals("0")) { count++ if (count == x) { break } } date += 1; } return date; }
傳入一個工作日天數,得到一個該天數工作日後的時間