JDK8 日期工具类DateUtils

/**
 * 日期工具类
 *
 * @author yangzihe
 * @date 2022/1/7
 */
@Slf4j
public class DateUtils {

    /**
     * 默认的日期时间格式 yyyy-MM-dd HH:mm:ss
     */
    private static final DateTimeFormatter DEFAULT_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 年月日的日期时间格式 yyyy-MM-dd
     */
    private static final DateTimeFormatter DAY_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /**
     * 时间戳 -> yyyy-MM-dd HH:mm:ss
     *
     * @param timestamp 时间戳
     *
     * @return 默认日期字符串 yyyy-MM-dd HH:mm:ss
     */
    public static String timestampToDefaultString(Long timestamp) {
        if (timestamp == null) {
            return null;
        }

        LocalDateTime localDateTime = timestampToLocalDateTime(timestamp);
        return DEFAULT_DATE_TIME_FORMATTER.format(localDateTime);
    }

    /**
     * 时间戳 -> yyyy-MM-dd
     *
     * @param timestamp 时间戳
     *
     * @return 年月日字符串 yyyy-MM-dd
     */
    public static String timestampToDayString(Long timestamp) {
        if (timestamp == null) {
            return null;
        }

        LocalDate localDate = timestampToLocalDate(timestamp);
        return DAY_DATE_TIME_FORMATTER.format(localDate);
    }

    /**
     * Date -> yyyy-MM-dd HH:mm:ss
     *
     * @param date Date
     *
     * @return 默认日期字符串 yyyy-MM-dd HH:mm:ss
     */
    public static String dateToDefaultString(Date date) {
        if (date == null) {
            return null;
        }

        LocalDateTime localDateTime = dateToLocalDateTime(date);
        return DEFAULT_DATE_TIME_FORMATTER.format(localDateTime);
    }

    /**
     * Date -> yyyy-MM-dd
     *
     * @param date Date
     *
     * @return 年月日字符串 yyyy-MM-dd
     */
    public static String dateToDayString(Date date) {
        if (date == null) {
            return null;
        }

        LocalDate localDate = dateToLocalDate(date);
        return DAY_DATE_TIME_FORMATTER.format(localDate);
    }

    /**
     * 日期增加
     *
     * @param date        Date
     * @param amountToAdd 增加的数量
     * @param unit        单位 秒、分钟、小时、天、星期、月、年
     *
     * @return 增加后的日期
     */
    public static Date plus(Date date, Long amountToAdd, ChronoUnit unit) {
        if (date == null || amountToAdd == null || unit == null) {
            return null;
        }

        LocalDateTime localDateTime = dateToLocalDateTime(date);
        LocalDateTime plusLocalDateTime = localDateTime.plus(amountToAdd, unit);
        return localDateTimeToDate(plusLocalDateTime);
    }

    /**
     * 日期减去
     *
     * @param date             Date
     * @param amountToSubtract 减去的数量
     * @param unit             单位 秒、分钟、小时、天、星期、月、年
     *
     * @return 减去后的日期
     */
    public static Date minus(Date date, Long amountToSubtract, ChronoUnit unit) {
        if (date == null || amountToSubtract == null || unit == null) {
            return null;
        }

        LocalDateTime localDateTime = dateToLocalDateTime(date);
        LocalDateTime minusLocalDateTime = localDateTime.minus(amountToSubtract, unit);
        return localDateTimeToDate(minusLocalDateTime);
    }

    /**
     * Date -> 当天0点时间戳 00:00:00
     *
     * @param date Date
     *
     * @return 当天0点时间戳
     */
    public static Long dateToZeroTimestamp(Date date) {
        if (date == null) {
            return null;
        }

        LocalDate localDate = dateToLocalDate(date);
        return localDateTimeToTimestamp(LocalDateTime.of(localDate, LocalTime.MIN));
    }

    /**
     * Date -> 当天结束时间戳 23:59:59
     *
     * @param date Date
     *
     * @return 当天结束时间戳
     */
    public static Long dateToDayEndTimestamp(Date date) {
        if (date == null) {
            return null;
        }

        LocalDate localDate = dateToLocalDate(date);
        return localDateTimeToTimestamp(LocalDateTime.of(localDate, LocalTime.MAX));
    }

    /**
     * timestamp -> 当天0点时间戳
     *
     * @param timestamp 时间戳
     *
     * @return 当天0点时间戳
     */
    public static Long timestampToZeroTimestamp(Long timestamp) {
        if (timestamp == null) {
            return null;
        }

        LocalDate localDate = timestampToLocalDate(timestamp);
        return localDateTimeToTimestamp(LocalDateTime.of(localDate, LocalTime.MIN));
    }

    /**
     * timestamp -> 当天结束时间戳 23:59:59
     *
     * @param timestamp 时间戳
     *
     * @return 当天结束时间戳
     */
    public static Long timestampToDayEndTimestamp(Long timestamp) {
        if (timestamp == null) {
            return null;
        }

        LocalDate localDate = timestampToLocalDate(timestamp);
        return localDateTimeToTimestamp(LocalDateTime.of(localDate, LocalTime.MAX));
    }

    /**
     * 日期间隔
     *
     * @param start 开始日期
     * @param end   结束日期
     * @param unit  单位 秒、分钟、小时、天、星期、月、年
     *
     * @return 间隔值
     */
    public static Long interval(Date start, Date end, ChronoUnit unit) {
        if (start == null || end == null || unit == null) {
            return null;
        }

        LocalDateTime startLocalDateTime = dateToLocalDateTime(start);
        LocalDateTime endLocalDateTime = dateToLocalDateTime(end);
        return startLocalDateTime.until(endLocalDateTime, unit);
    }

    /**
     * Date -> LocalDateTime
     *
     * @param date Date
     *
     * @return LocalDateTime
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        if (date == null) {
            return null;
        }

        Instant instant = date.toInstant();
        // 系统默认时区:当前容器时区都是统一的东8区
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    /**
     * Date -> LocalDate
     *
     * @param date Date
     *
     * @return LocalDate
     */
    public static LocalDate dateToLocalDate(Date date) {
        if (date == null) {
            return null;
        }

        Instant instant = date.toInstant();
        // 系统默认时区:当前容器时区都是统一的东8区
        ZoneId zone = ZoneId.systemDefault();
        return instant.atZone(zone).toLocalDate();
    }

    /**
     * 时间戳 -> LocalDate
     *
     * @param timestamp Date
     *
     * @return LocalDate
     */
    public static LocalDate timestampToLocalDate(Long timestamp) {
        if (timestamp == null) {
            return null;
        }

        // 系统默认时区:当前容器时区都是统一的东8区
        ZoneId zone = ZoneId.systemDefault();
        return Instant.ofEpochMilli(timestamp).atZone(zone).toLocalDate();
    }

    /**
     * 时间戳 -> LocalDateTime
     *
     * @param timestamp 时间戳
     *
     * @return LocalDateTime
     */
    public static LocalDateTime timestampToLocalDateTime(Long timestamp) {
        if (timestamp == null) {
            return null;
        }

        Instant instant = Instant.ofEpochMilli(timestamp);
        // 系统默认时区:当前容器时区都是统一的东8区
        ZoneId zoneId = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zoneId);
    }

    /**
     * LocalDateTime -> 时间戳
     *
     * @param localDateTime LocalDateTime
     *
     * @return 时间戳
     */
    public static Long localDateTimeToTimestamp(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }

        // 系统默认时区:当前容器时区都是统一的东8区
        ZoneId zoneId = ZoneId.systemDefault();
        return localDateTime.atZone(zoneId).toInstant().toEpochMilli();
    }

    /**
     * LocalDateTime -> Date
     *
     * @param localDateTime LocalDateTime
     *
     * @return Date
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }

        // 系统默认时区:当前容器时区都是统一的东8区
        ZoneId zoneId = ZoneId.systemDefault();
        return Date.from(localDateTime.atZone(zoneId).toInstant());
    }

    /**
     * Date -> 时间戳
     *
     * @param date 日期
     *
     * @return 时间戳
     */
    public static Long getTimestampByDate(Date date) {
        return date == null ? null : date.getTime();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值