日期转换工具类

  1. 简单说明
    1. 基于jdk1.8版本编写、测试;
    2. 使用jdk1.8新日期、时间API;
  2. 支持情况
    1. LocalDateTime -> StringString -> LocalDateTime 的互转
    2. String -> LongLong -> String 的互转
    3. LocalDateTime -> Long 与 Long -> LocalDateTime 的互转
    4. 基于LocalDateTime的日期类型比较(isAfter/isBefore/isEqual)
  3. 具体代码
import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 *
 * @author 杨彭伟
 * @date 2019-09-24 14:32
 */
public class DateTimeUtil {
    public static String DATE_TIME_EN = "yyyy-MM-dd HH:mm:ss";
    public static String DATE_TIME_ZH = "yyyy年MM月dd日 HH时mm分ss秒";
    public static String DATE_EN = "yyyy-MM-dd";
    public static String DATE_ZH = "yyyy年MM月dd日";
    public static String TIME_EN = "HH:mm:ss";
    public static String TIME_ZH = "HH时mm分ss秒";
    public static DateTimeFormatter defaultFormatter;

    static {
        defaultFormatter = DateTimeFormatter.ofPattern(DATE_TIME_EN);
    }

    /**
     * LocalDateTime -> String
     * @param date    时间
     * @param pattern 字符串格式
     * @return
     */
    public static String format(LocalDateTime date, String pattern) {
        return  DateTimeFormatter.ofPattern(pattern).format(date);
    }

    /**
     * LocalDateTime -> String
     * @param date 时间
     * @return
     */
    public static String format(LocalDateTime date) {
        return defaultFormatter.format(date);
    }

    /**
     * String -> LocalDateTime
     * @param date    时间
     * @param pattern 字符串格式
     * @return
     */
    public static LocalDateTime parse(String date, String pattern) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(date, dtf);
    }

    /**
     * String -> LocalDateTime
     * @param date 时间
     * @return
     */
    public static LocalDateTime parse(String date) {
        return LocalDateTime.parse(date, defaultFormatter);
    }

    /**
     * String -> Long
     * @param date    时间
     * @param pattern 字符串格式
     * @return
     */
    public static Long parseSecond(String date, String pattern) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        return getSecond(LocalDateTime.parse(date, dtf));
    }

    /**
     * String -> Long
     * @param date 时间
     * @return
     */
    public static Long parseSecond(String date) {
        return getSecond(LocalDateTime.parse(date, defaultFormatter));
    }

    /**
     * String -> Long
     * @param date    时间
     * @param pattern 字符串格式
     * @return
     */
    public static Long parseMilli(String date, String pattern) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        return getMilli(LocalDateTime.parse(date, dtf));
    }

    /**
     * String -> Long
     * @param date 时间
     * @return
     */
    public static Long parseMilli(String date) {
        return getMilli(LocalDateTime.parse(date, defaultFormatter));
    }

    /**
     * LocalDateTime -> Long(秒级)
     * @param date 时间
     * @return
     */
    public static Long getSecond(LocalDateTime date) {
        return date.toInstant(OffsetDateTime.now().getOffset()).getEpochSecond();
    }

    /**
     * LocalDateTime -> Long(毫秒级)
     * @param date 时间
     * @return
     */
    public static Long getMilli(LocalDateTime date) {
        return date.toInstant(OffsetDateTime.now().getOffset()).toEpochMilli();
    }

    /**
     * Long -> LocalDateTime (自动判断毫秒级/秒级)
     * @param date
     * @return
     */
    public static LocalDateTime getLocalDateTime(Long date) {
        // 获取date的位数
        int i = Long.toString(date).length();
        if (i == 13) {
            return LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
        } else if (i == 10) {
            return LocalDateTime.ofInstant(Instant.ofEpochSecond(date), ZoneId.systemDefault());
        } else {
            throw new DateTimeException("不支持");
        }
    }

    /**
     * Long -> String (自动判断毫秒级/秒级)
     * @param date
     * @param pattern 字符串格式
     * @return
     */
    public static String getDateString(Long date, String pattern) {
        LocalDateTime localDateTime = getLocalDateTime(date);
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * Long -> String (自动判断毫秒级/秒级)
     * @param date
     * @return
     */
    public static String getDateString(Long date) {
        return getLocalDateTime(date).format(defaultFormatter);
    }

    public static boolean isAfter(LocalDateTime date, LocalDateTime other) {
        return date.isAfter(other);
    }

    public static boolean isBefore(LocalDateTime date, LocalDateTime other) {
        return date.isBefore(other);
    }

    public static boolean isEqual(LocalDateTime date, LocalDateTime other) {
        return date.isEqual(other);
    }

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值