package com.ydtech.modules.partner.utils; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Date; public class PartnerDateUtils { public static boolean isWithinDays(Date date, int days) { LocalDate targetDate = date.toInstant() .atZone(ZoneId.systemDefault()) .toLocalDate(); return Math.abs(ChronoUnit.DAYS.between(LocalDate.now(), targetDate)) <= days; } public static Date addDaysToday(Date date, int days) { LocalDateTime localDate = date.toInstant() .atZone(ZoneId.systemDefault()) .toLocalDateTime(); LocalDateTime newLocalDate = localDate.plusDays(days); LocalDateTime newDateTime = newLocalDate.withHour(0) .withMinute(0) .withSecond(0) .withNano(0); return Date.from(newDateTime.atZone(ZoneId.systemDefault()).toInstant()); } public static Date addDays(Date date, int days) { LocalDateTime localDate = date.toInstant() .atZone(ZoneId.systemDefault()) .toLocalDateTime(); LocalDateTime newLocalDate = localDate.plusDays(days); LocalDateTime endOfDay = newLocalDate.withHour(23) .withMinute(59) .withSecond(59) .withNano(999_999_999); return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant()); } /** * 目标日期在今天之后 * @param date * @return */ public static boolean isAfter(Date date){ LocalDateTime localDate = date.toInstant() .atZone(ZoneId.systemDefault()) .toLocalDateTime(); return localDate.isAfter(LocalDateTime.now()); } public static boolean before(Date date){ if (date.before( new Date())) { System.out.println("目标时间在当前时间之前"); return true; } else { System.out.println("目标时间在当前时间之后或相同"); return false; } } /** * 目标日期在今天之前 * @param date * @return */ public static boolean isBefore(Date date){ LocalDateTime localDate = date.toInstant() .atZone(ZoneId.systemDefault()) .toLocalDateTime(); return localDate.isBefore(LocalDateTime.now()); } /** * 描述: 计算两个日期之间的天数 * @param date * @return */ public static Long dateDifference(LocalDate date){ return ChronoUnit.DAYS.between(LocalDate.now(),date); } public static Long daysBetween (LocalDate date){ return ChronoUnit.DAYS.between(date,LocalDate.now()); } public static boolean isSameDay(Date date) { LocalDate currentDate = LocalDate.now(); LocalDate compareLocalDate = date.toInstant() .atZone(ZoneId.systemDefault()) .toLocalDate(); return currentDate.equals(compareLocalDate.plusDays(1)); } /** * 描述: 生成到期时间 * @param startTime * @param khDays * @return */ public static Date generateEndTime(Date startTime,Integer khDays) { LocalDateTime localDateTime = startTime.toInstant() .atZone(ZoneId.systemDefault()) .toLocalDateTime(); LocalDateTime resultDateTime = localDateTime.plusDays(khDays).minusSeconds(1); return Date.from(resultDateTime.atZone(ZoneId.systemDefault()).toInstant()); } public static String timeExample(Integer khDays) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate minus = LocalDate.now().minus(khDays, ChronoUnit.DAYS); return formatter.format( minus); } }