LocalDateTime 格式的起止时间

public class LocalDateTimeUtil extends cn.hutool.core.date.LocalDateTimeUtil {

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

public static long between(LocalDate startTimeInclude, LocalDate endTimeExclude, ChronoUnit unit) { return between(LocalDateTime.of(startTimeInclude, LocalTime.MIN), LocalDateTime.of(endTimeExclude, LocalTime.MIN), unit); } /** * t1 是否晚于 t2 * * @param t1 * @param t2 * @return */ public static boolean isAfter(LocalDateTime t1, LocalDateTime t2) { return t1.compareTo(t2) > 0; } /** * t1 是否晚于 t2 * * @param t1 * @param t2 * @return */ public static boolean isAfter(LocalDate t1, LocalDate t2) { return t1.compareTo(t2) > 0; } /** * t1 是否晚于 t2 * * @param t1 * @param t2 * @return */ public static boolean isAfterEqual(LocalDateTime t1, LocalDateTime t2) { return t1.compareTo(t2) >= 0; } /** * t1 是否晚于 t2 * * @param t1 * @param t2 * @return */ public static boolean isAfterEqual(LocalDate t1, LocalDate t2) { return t1.compareTo(t2) >= 0; } /** * t1 是否早于 t2 * * @param t1 * @param t2 * @return */ public static boolean isBefore(LocalDate t1, LocalDate t2) { return t1.compareTo(t2) < 0; } /** * t1 是否早于 t2 * * @param t1 * @param t2 * @return */ public static boolean isBefore(LocalDateTime t1, LocalDateTime t2) { return t1.compareTo(t2) < 0; } /** * t1 是否早于 t2 * * @param t1 * @param t2 * @return */ public static boolean isBeforeEqual(LocalDateTime t1, LocalDateTime t2) { return t1.compareTo(t2) <= 0; } /** * t1 是否早于 t2 * * @param t1 * @param t2 * @return */ public static boolean isBeforeEqual(LocalDate t1, LocalDate t2) { return t1.compareTo(t2) <= 0; } /** * 获取服务器启动时间 */ public static LocalDateTime getServerStartDate() { long time = ManagementFactory.getRuntimeMXBean().getStartTime(); return LocalDateTimeUtil.of(time); } /** * 日期型字符串转化为日期 格式 */ public static LocalDateTime parseOldDate(Object str) { if (str == null) { return null; } try { return LocalDateTimeUtil.of(org.apache.commons.lang3.time.DateUtils.parseDate(str.toString(), parsePatterns)); } catch (ParseException e) { return null; } } /** * 判断是否为同一天 * * @param startTime * @param endTime * @return */ public static boolean sameDay(LocalDateTime startTime, LocalDateTime endTime) { return sameDay(startTime.toLocalDate(), endTime.toLocalDate()); } /** * 判断是否为同一天 * * @param startTime * @param endTime * @return */ public static boolean sameDay(LocalDate startTime, LocalDate endTime) { return startTime.equals(endTime); } public static LocalDate getStartDayOfWeek(LocalDate today) { return getStartDayOfWeek(LocalDateTime.of(today, LocalTime.MIN)).toLocalDate(); } public static LocalDate getEndDayOfWeek(LocalDate today) { return getEndDayOfWeek(LocalDateTime.of(today, LocalTime.MAX)).toLocalDate(); } /** * 获取 日期 本周的开始时间 * * @param today * @return */ public static LocalDateTime getStartDayOfWeek(LocalDateTime today) { LocalDateTime resDate = LocalDateTime.now(); if (today == null) { today = resDate; } DayOfWeek week = today.getDayOfWeek(); int value = week.getValue(); resDate = today.minusDays(value - 1); return LocalDateTime.of(resDate.toLocalDate(), LocalTime.MIN); } /** * 获取 日期 本周的结束时间 * * @param today * @return */ public static LocalDateTime getEndDayOfWeek(LocalDateTime today) { LocalDateTime resDate = LocalDateTime.now(); if (today == null) { today = resDate; } DayOfWeek week = today.getDayOfWeek(); int value = week.getValue(); resDate = today.plusDays(7 - value); return LocalDateTime.of(resDate.toLocalDate(), LocalTime.MAX); } }

 

posted @ 2021-04-13 11:18  bookc  阅读(565)  评论(0编辑  收藏  举报