代码片段:基于 JDK 8 time包的时间工具类 TimeUtil

it2022-05-09  36

摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢!

“知识的工作者必须成为自己时间的首席执行官。”

前言

这次泥瓦匠带来的是一个好玩的基于 JDK 8 time包的时间工具类 TimeUtil。本意,用 JDK 8 写个实战的时间工具类,初版泥瓦匠写的很不好,后来雨神分分钟将这个代码优化优化,谢谢雨神。就此分享下这个代码,让更多的人看到~

一、 Java 8 time包

从 Java 1.0 有个 Date 类,想必大家不陌生,后面有了 Calendar 类(被废弃 )。API 确实比较难用,因此 Java 8 引入 java.time API,这次看看是不是很好用。大致引入了这几个对象:

Instant – 瞬间类,表示时间线上的一点(与 Date 类似)Duration – 持续时间,表示两个 Instant 之间的时间LocalDateTime – 本地日期时间,表示一个日期和时间。

本小文重点还是在使用 LocalDateTime 及其格式化类 DateTimeFormatter。

二、介绍 LocalDateTime & DateTimeFormatter APIs

LocalDateTime 表示一个日期和时间,存储确定时区中的某个时间点。例如某一次练书法弹琴活动。(ps:泥瓦匠有个小圈子,里面喜欢加入一些文艺的程序员。爱技术,爱生活,爱艺术~ 雨神就爱弹琴,日语思密达*&#@#% )

常用api:

now()从系统中获取当前时间parse(CharSequence text, DateTimeFormatter formatter)从字符串按格式获取 LocalDateTime 实例

DateTimeFormatter 用于格式化时间,提供了公用的方法入口,打印和解析格式化的时间类。

常用api:

format(TemporalAccessor temporal)按格式格式化时间ofPattern(String pattern)按字符串指定的格式,生成时间格式

 

三、TimeUtil 代码详解

泥瓦匠一一道来这个代码的实现。先看代码:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;   /**   * 基于 JDK 8 time包的时间工具类   * <p/>   * Created by bysocket on 16/8/23.   */ public final class TimeUtil {        /**       * 获取默认时间格式: yyyy-MM-dd HH:mm:ss       */      private static final DateTimeFormatter DEFAULT_DATETIME_FORMATTER = TimeFormat.LONG_DATE_PATTERN_LINE.formatter;        private TimeUtil() {          // no construct function      }        /**       * String 转时间       *       * @param timeStr       * @return       */      public static LocalDateTime parseTime(String timeStr) {          return LocalDateTime.parse(timeStr, DEFAULT_DATETIME_FORMATTER);      }        /**       * String 转时间       *       * @param timeStr       * @param format  时间格式       * @return       */      public static LocalDateTime parseTime(String timeStr, TimeFormat format) {          return LocalDateTime.parse(timeStr, format.formatter);      }        /**       * 时间转 String       *       * @param time       * @return       */      public static String parseTime(LocalDateTime time) {          return DEFAULT_DATETIME_FORMATTER.format(time);      }        /**       * 时间转 String       *       * @param time       * @param format 时间格式       * @return       */      public static String parseTime(LocalDateTime time, TimeFormat format) {          return format.formatter.format(time);      }        /**       * 获取当前时间       *       * @return       */      public static String getCurrentDatetime() {          return DEFAULT_DATETIME_FORMATTER.format(LocalDateTime.now());      }        /**       * 获取当前时间       *       * @param format 时间格式       * @return       */      public static String getCurrentDatetime(TimeFormat format) {          return format.formatter.format(LocalDateTime.now());      }        /**       * 时间格式       */      public enum TimeFormat {            /**           * 短时间格式           */          SHORT_DATE_PATTERN_LINE( "yyyy-MM-dd" ),          SHORT_DATE_PATTERN_SLASH( "yyyy/MM/dd" ),          SHORT_DATE_PATTERN_DOUBLE_SLASH( "yyyy\\MM\\dd" ),          SHORT_DATE_PATTERN_NONE( "yyyyMMdd" ),            /**           * 长时间格式           */          LONG_DATE_PATTERN_LINE( "yyyy-MM-dd HH:mm:ss" ),          LONG_DATE_PATTERN_SLASH( "yyyy/MM/dd HH:mm:ss" ),          LONG_DATE_PATTERN_DOUBLE_SLASH( "yyyy\\MM\\dd HH:mm:ss" ),          LONG_DATE_PATTERN_NONE( "yyyyMMdd HH:mm:ss" ),            /**           * 长时间格式 带毫秒           */          LONG_DATE_PATTERN_WITH_MILSEC_LINE( "yyyy-MM-dd HH:mm:ss.SSS" ),          LONG_DATE_PATTERN_WITH_MILSEC_SLASH( "yyyy/MM/dd HH:mm:ss.SSS" ),          LONG_DATE_PATTERN_WITH_MILSEC_DOUBLE_SLASH( "yyyy\\MM\\dd HH:mm:ss.SSS" ),          LONG_DATE_PATTERN_WITH_MILSEC_NONE( "yyyyMMdd HH:mm:ss.SSS" );            private transient DateTimeFormatter formatter;            TimeFormat(String pattern) {              formatter = DateTimeFormatter.ofPattern(pattern);          }      } }

工具类由 final TimeUtil类 及 其内部枚举类TimeFormat时间格式类 组成。1/ TimeUtil 具有私有构造函数,表示被保护,无法被外部 new 出实例。

声明了默认的  DateTimeFormatter 时间格式:yyyy-MM-dd HH:mm:ss。其他则是提供了 获取当前时间 和 时间与String互转的方法。2/ TimeFormat 内部枚举类,首先它是单例的。

transient 关键字目的是确保 DateTimeFormatter 无序列化存储。为啥单例,因为 DateTimeFormmatter 是无状态的,可以线程共享。

具体方法解析如下:1. 获取当前时间

1 2 3 4 String now = TimeUtil.getCurrentDatetime(); System.out.println(now); output: 2016 - 08 - 28 16 : 35 : 23

2. 获取当前相应格式的当前时间

1 2 3 4 String now = TimeUtil.getCurrentDatetime(TimeUtil.TimeFormat.LONG_DATE_PATTERN_SLASH); System.out.println(now); output: 2016 / 08 / 28 16 : 36 : 24

3. String 转时间默认格式:yyyy-MM-dd HH:mm:ss

1 2 3 LocalDateTime expectedDateTime = LocalDateTime.of( 2014 , 11 , 11 , 10 , 11 , 11 ); LocalDateTime parsedTime = TimeUtil.parseTime( "2014-11-11 10:11:11" ); assertEquals(expectedDateTime, parsedTime);

其他格式之一:yyyy-MM-dd HH:mm:ss

1 2 3 LocalDateTime expectedDateTime = LocalDateTime.of( 2014 , 11 , 11 , 10 , 11 , 11 ); LocalDateTime parsedTime = TimeUtil.parseTime( "2014/11/11 10:11:11" , LONG_DATE_PATTERN_SLASH); assertEquals(expectedDateTime, parsedTime);

4. 时间转 String默认格式:yyyy-MM-dd HH:mm:ss

1 2 LocalDateTime time = LocalDateTime.of( 2014 , 11 , 11 , 10 , 11 , 11 ); assertEquals(TimeUtil.parseTime(time), "2014-11-11 10:11:11" );

其他格式之一:yyyy-MM-dd HH:mm:ss

1 2 LocalDateTime time = LocalDateTime.of( 2014 , 11 , 11 , 10 , 11 , 11 ); assertEquals(TimeUtil.parseTime(time, LONG_DATE_PATTERN_DOUBLE_SLASH), "2014\\11\\11 10:11:11" );

 

四、与 Old 代码互操作

java.time 类与 Old Date 代码互操作如下图:

 

五、小结

实战中的 JDK8 ,Show the code。 在用的项目,完善测试时候用起来才是关键。自然需要 JDK 环境支持,升级吧升级吧。基于 JDK 8 time包的实践,这次只讲了 LocalDateTime 类,慢慢带来更多。相关代码分享在 Github 主页

如以上文章或链接对你有帮助的话,别忘了在文章结尾处评论哈~ 你也可以点击页面右边“分享”悬浮按钮哦,让更多的人阅读这篇文章。

转载于:https://www.cnblogs.com/Alandre/p/5816946.html

相关资源:数据结构—成绩单生成器

最新回复(0)