| | |
| | | import java.util.Date; |
| | | import java.util.GregorianCalendar; |
| | | import java.util.TimeZone; |
| | | import java.util.function.Consumer; |
| | | |
| | | import org.jeecg.common.constant.SymbolConstant; |
| | | import org.springframework.util.StringUtils; |
| | |
| | | calendar.setTime(getDate()); |
| | | return calendar.get(Calendar.YEAR); |
| | | } |
| | | |
| | | public static String getDayStr(Date date) { |
| | | Calendar calendar = getCalendar(); |
| | | calendar.setTime(date);// 把当前时间赋给日历 |
| | |
| | | String dayStr = day < 10 ? "0" + day : day + ""; |
| | | return dayStr; |
| | | } |
| | | |
| | | /** |
| | | * 将字符串转成时间 |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static Date parseDatetime(String str){ |
| | | public static Date parseDatetime(String str) { |
| | | try { |
| | | return datetimeFormat.get().parse(str); |
| | | }catch (Exception e){ |
| | | } catch (Exception e) { |
| | | } |
| | | return null; |
| | | } |
| | |
| | | /** |
| | | * 获取指定时间之后的几年 qsw |
| | | */ |
| | | public static Date getYearAfter(Date data,int number) { |
| | | public static Date getYearAfter(Date data, int number) { |
| | | Calendar c = Calendar.getInstance(); |
| | | c.setTime(data); |
| | | c.add(Calendar.YEAR, number); |
| | |
| | | /** |
| | | * 获取指定时间之后的几天 qsw |
| | | */ |
| | | public static Date getDayAfter(Date data,int number) { |
| | | public static Date getDayAfter(Date data, int number) { |
| | | Calendar c = Calendar.getInstance(); |
| | | c.setTime(data); |
| | | c.add(Calendar.DAY_OF_MONTH, number); |
| | |
| | | /** |
| | | * 获取指定时间之后的几分钟 qsw |
| | | */ |
| | | public static Date getMinAfter(Date data,int number) { |
| | | public static Date getMinAfter(Date data, int number) { |
| | | Calendar c = Calendar.getInstance(); |
| | | c.setTime(data); |
| | | c.add(Calendar.MINUTE, number); |
| | |
| | | /** |
| | | * 获取指定时间之后的几小时 qsw |
| | | */ |
| | | public static Date getHourAfter(Date data,int number) { |
| | | public static Date getHourAfter(Date data, int number) { |
| | | Calendar c = Calendar.getInstance(); |
| | | c.setTime(data); |
| | | c.add(Calendar.HOUR_OF_DAY, number); |
| | |
| | | public static Date localDateToDate(LocalDate localDate) { |
| | | return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 日期加年 |
| | | * @param date |
| | | * @param year |
| | | * @return |
| | | */ |
| | | public static Date addYear(Date date, int year) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.add(Calendar.YEAR, year); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 检查日期是否在今天或之前(忽略时间部分) |
| | | */ |
| | | public static boolean isBeforeOrEqualToday(Date targetDate) { |
| | | if (targetDate == null) return false; |
| | | |
| | | Calendar calTarget = Calendar.getInstance(); |
| | | calTarget.setTime(targetDate); |
| | | clearTime(calTarget); |
| | | |
| | | Calendar calToday = Calendar.getInstance(); |
| | | clearTime(calToday); |
| | | |
| | | return !calTarget.after(calToday); |
| | | } |
| | | |
| | | private static void clearTime(Calendar cal) { |
| | | cal.set(Calendar.HOUR_OF_DAY, 0); |
| | | cal.set(Calendar.MINUTE, 0); |
| | | cal.set(Calendar.SECOND, 0); |
| | | cal.set(Calendar.MILLISECOND, 0); |
| | | } |
| | | |
| | | public static void calculateMaintenanceDates(Date nextDate, Integer periodYears, |
| | | Consumer<Date> setNextMaintenance, |
| | | Consumer<Date> setLatestMaintenance) { |
| | | if (nextDate == null || periodYears == null || periodYears <= 0) { |
| | | return; |
| | | } |
| | | |
| | | // 转换为Java 8日期类型 |
| | | LocalDate nextLocal = nextDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); |
| | | LocalDate today = LocalDate.now(); |
| | | |
| | | // 确保有效的计算周期 |
| | | int period = Math.max(1, periodYears); // 防止传入0或负值 |
| | | |
| | | if (nextLocal.isAfter(today)) { |
| | | // 未过期:计算最近维护日期 |
| | | setLatestMaintenance.accept( |
| | | Date.from(nextLocal.minusYears(period).atStartOfDay() |
| | | .atZone(ZoneId.systemDefault()).toInstant()) |
| | | ); |
| | | } else { |
| | | // 已过期:计算新的下次维护日期 |
| | | while (!nextLocal.isAfter(today)) { |
| | | nextLocal = nextLocal.plusYears(period); |
| | | } |
| | | |
| | | // 设置新日期 |
| | | setNextMaintenance.accept( |
| | | Date.from(nextLocal.atStartOfDay() |
| | | .atZone(ZoneId.systemDefault()).toInstant()) |
| | | ); |
| | | |
| | | // 计算最近维护日期 |
| | | setLatestMaintenance.accept( |
| | | Date.from(nextLocal.minusYears(period).atStartOfDay() |
| | | .atZone(ZoneId.systemDefault()).toInstant()) |
| | | ); |
| | | } |
| | | } |
| | | } |