从实例入手java8时间和日期类

旧的java 时间处理Api存在的问题

线程安全: Date和Calendar不是线程安全的,你需要编写额外的代码处理线程安全问题
DateFormat方法也有它自己的问题。比如,它不是线程安全的 这意味着两个线程如果尝试使用同一个

formatter解析日期,你可能会得到无法预期的结果。

API设计和易用性: 由于Date和Calendar的设计不当你无法完成日常的日期操作 , 例如:2014年3月18日 Date date = new Date(114, 2, 18);

ZonedDate和Time: 你必须编写额外的逻辑处理时区和那些旧的逻辑

所有这些缺陷和不一致导致用户们转投第三方的日期和时间库,比如Joda-Time。为了解决这些问题,Oracle决定在原生的Java API中提供高质量的日期和时间支持。所以,你会看到Java 8在java.time包中整合了很多Joda-Time的特性

Joda-Time 基本工具使用

pom.xml 引入如下配置

<dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.3</version>
        </dependency>

完成常用工具方法

public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";


    public static Date strToDate(String dateTimeStr, String formatStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static String dateToStr(Date date, String formatStr) {
        if (date == null) {
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }

    public static Date strToDate(String dateTimeStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static String dateToStr(Date date) {
        if (date == null) {
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(STANDARD_FORMAT);
    }

java8时间处理Api基本使用


    //将LocalDateTime转为自定义的时间格式的字符串
    public static String transferDateTimeToStr(LocalDateTime localDateTime, String format) {
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }


    //将long类型的timestamp转为LocalDateTime
    public static LocalDateTime transferTimestampToLocalDateTime(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    //将LocalDateTime转为long类型的timestamp
    public static long transferLocalDateTimeToTimestamp(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    //将某时间字符串转为自定义时间格式的LocalDateTime
    public static LocalDateTime transferStrToLocalDateTime(String time, String format) {
        java.time.format.DateTimeFormatter df = java.time.format.DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(time, df);
    }

小确幸

每一丝灵感都值得被记录,每一笔记录都是成长,每一点成长都值得欢呼

博主个人站: www.imisty.cn
CSDN博客: https://blog.csdn.net/lookinthefog
博客园 :https://imist.cnblogs.com/

希望能够认识一些热爱技术的小伙伴,欢迎友链接哟

猜你喜欢

转载自www.cnblogs.com/imist/p/11417629.html