Java关于日期处理
-
获取今天日期(today)
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); String today = dateFormat.format(date);
-
获取明天日期(tomorrow)
Date today = new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); String yesterday = simpleDateFormat.format(today);//获取明天日期
-
获取昨天日期(yesterday)
Date today = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); String yesterday = simpleDateFormat.format(today);//获取昨天日期
-
判断是否是工作日
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 String dateType(String httpArg){ String jsonResult = request(httpArg); //String jsonResult = HolidayUtil.request("20220715"); // 0 上班 1周末 2节假日 String type = null; switch (jsonResult){ case "0": type = "工作日"; break; case "1": type = "周末"; break; case "2" : type = "节假日"; break; } return type; }
-
判断是否是节假日
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 String dateType(String httpArg){ String jsonResult = request(httpArg); //String jsonResult = HolidayUtil.request("20220715"); // 0 上班 1周末 2节假日 String type = null; switch (jsonResult){ case "0": type = "工作日"; break; case "1": type = "周末"; break; case "2" : type = "节假日"; break; } return type; }