欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

判断日期是否为工作日,休息日,节假日

程序员文章站 2022-03-03 10:01:35
...
package com.xyl;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;

public class demo15 {
	public static void main(String[] args) throws ParseException {
		
        String time="20181001";
        String dateFlag = getHoliday(time);
        if ("1".equals(dateFlag) || "2".equals(dateFlag)) {
            System.err.println("今天是法定节假日");
        }else {
            System.out.println("今天是工作日");
        }
}

   /**
	 * 
	 * 方法描述:方法描述:获取节假日 访问接口,根据返回值判断当前日期是否为工作日,
	 * 返回结果:检查具体日期是否为节假日,工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2;
	 * @author xyl
	 */
	public static  String  getHoliday(String  time) {
		String dc = "http://tool.bitefu.net/jiari/?d=";
		String  httpUrl=new StringBuffer().append(dc).append(time).toString();
		BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
		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);
			}
			reader.close();
			result = sbf.toString();
		} catch (Exception e) {
			
		}
		return result;
	}
}