判断当前时间是否在限制时间段之内
程序员文章站
2022-06-30 21:34:29
...
/** * 判断当前时间是否在限制时间段之内(是:返回true,否:返回false) * * @return */ public static boolean isLimitTime() { boolean bool = false; Date currentTime = com.born.sys.util.date.DateUtil.getDateHHObject(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); String currentDateStr = sdf.format(currentTime); String day = currentDateStr.substring(8, 10); String monthEndDay = DateUtil.getEndDateOfMonth(sdf1 .format(currentTime)); if ("01".equals(day)) { bool = true; } else if ("02".equals(day)) { String timeTemp = currentDateStr.substring(0, 10) + " 09:00:00"; try { Date endTime = sdf.parse(timeTemp); long interval = (currentTime.getTime() - endTime.getTime()) / 1000;// 秒 if (interval < 0) { bool = true; } } catch (ParseException e) { e.printStackTrace(); } } else { if (day.equals(monthEndDay.subSequence(8, 10))) { String timeTemp = monthEndDay + " 22:00:00"; try { Date endTime = sdf.parse(timeTemp); long interval = (currentTime.getTime() - endTime.getTime()) / 1000;// 秒 if (interval > 0) { bool = true; } } catch (ParseException e) { e.printStackTrace(); } } } return bool; } /** * 获取今天日期是1号还是2号或者月末最后一天, 1日:1 2日:2 月末:3 * * @return */ public static String getDayLimit() { String reStr = "0"; Date currentTime = com.born.sys.util.date.DateUtil.getDateHHObject(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); String currentDateStr = sdf.format(currentTime); String day = currentDateStr.substring(8, 10); String monthEndDay = DateUtil.getEndDateOfMonth(sdf1 .format(currentTime)); if ("01".equals(day)) { reStr = "1"; } else if ("02".equals(day)) { reStr = "2"; } else { if (day.equals(monthEndDay.subSequence(8, 10))) { reStr = "3"; } } return reStr; } /** * 获取一个月的最后一天 * * @param dat * @return */ public static String getEndDateOfMonth(String date) {// yyyy-MM-dd String str = date.substring(0, 8); String month = date.substring(5, 7); int mon = Integer.parseInt(month); if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) { str += "31"; } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) { str += "30"; } else { if (isLeapYear(date)) { str += "29"; } else { str += "28"; } } return str; } /** * 判断是否润年 * * @param ddate * @return */ public static boolean isLeapYear(String ddate) { Date d = com.born.sys.util.date.DateUtil.getDateHHObject(); GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); gc.setTime(d); int year = gc.get(Calendar.YEAR); if ((year % 400) == 0) return true; else if ((year % 4) == 0) { if ((year % 100) == 0) return false; else return true; } else return false; }