栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

日期转换工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

日期转换工具类

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


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);
    }

    
    public static String format(LocalDateTime date, String pattern) {
        return  DateTimeFormatter.ofPattern(pattern).format(date);
    }

    
    public static String format(LocalDateTime date) {
        return defaultFormatter.format(date);
    }

    
    public static LocalDateTime parse(String date, String pattern) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(date, dtf);
    }

    
    public static LocalDateTime parse(String date) {
        return LocalDateTime.parse(date, defaultFormatter);
    }

    
    public static Long parseSecond(String date, String pattern) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        return getSecond(LocalDateTime.parse(date, dtf));
    }

    
    public static Long parseSecond(String date) {
        return getSecond(LocalDateTime.parse(date, defaultFormatter));
    }

    
    public static Long parseMilli(String date, String pattern) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        return getMilli(LocalDateTime.parse(date, dtf));
    }

    
    public static Long parseMilli(String date) {
        return getMilli(LocalDateTime.parse(date, defaultFormatter));
    }

    
    public static Long getSecond(LocalDateTime date) {
        return date.toInstant(OffsetDateTime.now().getOffset()).getEpochSecond();
    }

    
    public static Long getMilli(LocalDateTime date) {
        return date.toInstant(OffsetDateTime.now().getOffset()).toEpochMilli();
    }

    
    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("不支持");
        }
    }

    
    public static String getDateString(Long date, String pattern) {
        LocalDateTime localDateTime = getLocalDateTime(date);
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    
    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);
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/709409.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号