最近工作需要定時器發佈任務,但是定時器發佈任務是指定日期,有時候涉及到節假日,這裏就需要過濾掉節假日了,所以二話不說直接上代碼
code=2調休 code=1正常的週六日 code=0工作日
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import javax.net.ssl.*;
import java.io.*;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
/*
* 判斷當前時間是不是工作日
* */
public Boolean getData(){
SimpleDateFormat dft2 = new SimpleDateFormat("yyyyMMdd");
String newDate=dft2.format(new Date());
String addess = "http://api.goseek.cn/Tools/holiday?date="+newDate;
URL url = null;
HttpURLConnection httpConn = null;
BufferedReader in = null;
StringBuffer sb = new StringBuffer();
try{
url = new URL(addess);
in = new BufferedReader(new InputStreamReader(url.openStream(),"utf-8") );
String str = null;
while((str = in.readLine()) != null) {
sb.append( str );
}
} catch (Exception ex) {
} finally{
try{
if(in!=null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
String data =sb.toString();
JSONObject json = getjson(data);
String value=json.get("data").toString();
System.out.println(value);
if(value.equals("0"))
return true;
else
return false;
}
//json串轉化爲json對象
public JSONObject getjson(String data) {
JSONObject json = JSONObject.fromObject(data);
return json;
}