该方法的实现是通过调用一个网上的大佬的线上API(http://tool.bitefu.net/jiari/)实现的,所以该方法的实现需要联网才可以。
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.Calendar; public class HolidayUtil { /** * 判断今日是否为工作日 * @param httpArg * @return */ public static String request(String httpArg) { String httpUrl = "http://tool.bitefu.net/jiari/"; BufferedReader reader = null; String result = null; StringBuffer sbf = new StringBuffer(); httpUrl = httpUrl + "?d=" +httpArg; 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) { e.printStackTrace(); } return result; } public static String judgeDate() { //获取当前时间,格式为yyyyMMdd String woringDays = DateGestalt.formatDate(Calendar.getInstance(), "yyyyMMdd"); String jsonResult = HolidayUtil.request(woringDays); // 0 上班 1周末 2节假日 if ("0".equals(jsonResult)) { woringDays = "0"; } if ("1".equals(jsonResult)) { woringDays = "1"; } if ("2".equals(jsonResult)) { woringDays = "2"; } return woringDays; } }