查询是否为节假日

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值