public class test {
public static void main(String[] args) {
getWeektype(Judgedate);//这里填入要判断的日期,Judgedate为String类型
}
/***
* 判断日期类型
*
* @param Judgedate 想要判断的日期
* @return 工作日/法定节假日休息/正常休息日
* @author cyz
*/
public static String getWeektype(String Judgedate) {
JSONObject json = new JSONObject();
String date_str = isHoliday(Judgedate);
json = JSONObject.fromObject(date_str);
String datelx = (json.get(“data”).toString());
if (“0”.equals(datelx)) {
return “工作日”;
} else if (“1”.equals(datelx)) {
return “法定节假日休息”;
} else if (“2”.equals(datelx)) {
return “法定节假日休息”;
} else if (“3”.equals(datelx)) {
return “正常休息日”;
}
return null;
}
//访问url并获得值
public static String sendGet(String url) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestProperty("Content-Type", "text/html");
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("user-agent", "Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.connect();
// 定义BUffer额度Reader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
}
//是使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
/***
* 判断网络上的api来判断日期的类型
*
* @param Judgedate
* @return json格式的类型,其中包括是否是节假日,通过data中的值来判断
* @author cyz
*/
public static String isHoliday(String Judgedate) {
String url = "http://api.goseek.cn/Tools/holiday?date=" + Judgedate;
String result = sendGet(url);
return result;
}
}
//注:需要导入jar包
jar包为:import net.sf.json.JSONObject;
参考文献:https://blog.csdn.net/hcwbr123/article/details/79472596