zoukankan      html  css  js  c++  java
  • 从实例入手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/

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

  • 相关阅读:
    不重复随机数生成
    centos 输入密码正确进不去系统
    程序退出异常_DebugHeapDelete和std::numpunct
    iptables导致数据包过多时连接失败
    linux服务器并发与tcmalloc
    Windows server 2008 被ntlmssp安装攻击 解决
    转载 html div三列布局占满全屏(左右两列定宽或者百分比、中间自动适应,div在父div中居底)
    WIN2003使用IP安全策略只允许指定IP远程桌面连接
    如何让电脑公司Win7系统自动关闭停止响应的程序
    win7 64的系统安装。net4.0总是提示安装未成功
  • 原文地址:https://www.cnblogs.com/imist/p/11417629.html
Copyright © 2011-2022 走看看