通過Java日期時間API系列1-----Jdk7及以前的日期時間類中得知,Java8以前除了java.sql.Timestamp擴充納秒,其他類只精確到毫秒;Java8 time包所有相關類都支持納秒,
所有時間類秒後,只有納秒字段,沒有毫秒和微秒的字段,下面介紹如果格式化和解析微秒和納秒。
1.Timestamp格式化中的納秒。
java.sql.Timestamp.toString()方法中的格式爲:
格式化模板是:yyyy-mm-dd hh:mm:ss.fffffffff,這裏的 fffffffff 指的是納秒,但會將最後的0省略掉。比如:
2020-05-23 17:06:30.0
2020-05-23 17:06:30.272
2020-05-23 17:06:30.27215
2020-05-23 17:06:30.27215062
2.Timestamp格式解析
.
/** * 解析Timestamp格式字符串爲LocalDateTime 默認格式 yyyy-mm-dd hh:mm:ss.fffffffff 其中 fffffffff 納秒,省略後面的0 比如: * <pre> * 2020-05-23 17:06:30.0 * 2020-05-23 17:06:30.272 * 2020-05-23 17:06:30.27215 * 2020-05-23 17:06:30.27215062 *</pre> * @param text * @return LocalDateTime */ public static LocalDateTime parseTimestampStyleToLocalDateTime(String text){ //預處理 Objects.requireNonNull(text, "text"); text=text.trim(); if(!text.contains(".")){ throw new DateTimeException("text is not supported! " + text); } //.分割成2部分,分別分析 String[] textArr = text.split("\\."); String main = textArr[0]; String nanoOfSecond = textArr[1]; int mainLen = main.length(); int len = nanoOfSecond.length(); if(mainLen != DateFormatPattern.YYYY_MM_DD_HH_MM_SS.length()){ throw new DateTimeException("text is not supported! " + text); } if(len>9){ throw new DateTimeException("text is not supported! " + text); } //納秒部分補0 StringBuilder sb = new StringBuilder(); for(int i=0;i<9-len;i++){ sb.append("0"); } nanoOfSecond = nanoOfSecond+sb.toString(); text = main+"."+nanoOfSecond; //使用yyyy-MM-dd HH:mm:ss.SSSSSSSSS 標準格式解析 return parseToLocalDateTime(text, DateTimeFormatterUtil.YYYY_MM_DD_HH_MM_SS_SSSSSSSSS_FMT); }
測試:
/** * 含納秒Timestamp時間格式解析 */ @Test public void parseTimestampStyleTest(){ Date date = DateTimeFormatterUtil.smartParseToDate("2020-05-23 17:06:30"); Date date2 = DateTimeFormatterUtil.parseTimestampStyleToDate("2020-05-23 17:06:30.0"); Date date3 = DateTimeFormatterUtil.parseTimestampStyleToDate("2020-05-23 17:06:30.0"); Assert.assertEquals(date, date2); Assert.assertEquals(date2, date3); LocalDateTime localDateTime1 = DateTimeFormatterUtil.smartParseToLocalDateTime("2020-05-23 17:06:30.272150"); LocalDateTime localDateTime2 = DateTimeFormatterUtil.smartParseToLocalDateTime("2020-05-23 17:06:30.27215"); Assert.assertEquals(localDateTime1, localDateTime2); }
3.微秒和納秒的格式化和解析
// ==================================yyyy-MM-dd HH:mm:ss.SSSSSS 相關Pattern================================== /** * yyyy-MM-dd HH:mm:ss.SSSSSS 比如:2020-05-23 17:06:30.272150 */ public static final String YYYY_MM_DD_HH_MM_SS_SSSSSS = "yyyy-MM-dd HH:mm:ss.SSSSSS"; // ==================================yyyy-MM-dd HH:mm:ss.SSSSSSSSS 相關Pattern================================== /** * yyyy-MM-dd HH:mm:ss.SSSSSSSSS 比如:2020-05-23 17:06:30.272150620 */ public static final String YYYY_MM_DD_HH_MM_SS_SSSSSSSSS = "yyyy-MM-dd HH:mm:ss.SSSSSSSSS";
(1)格式化
/** * 含納秒時間格式化 */ @Test public void formatTimeWithNanoTest(){ LocalDateTime localDateTime = DateTimeFormatterUtil.smartParseToLocalDateTime("2020-05-23T17:06:30.272150620+08:00"); //時間格式化 Assert.assertEquals("17:06:30.272", DateTimeFormatterUtil.format(localDateTime, DateTimeFormatterUtil.HH_MM_SS_SSS_FMT)); Assert.assertEquals("17:06:30.272150", DateTimeFormatterUtil.format(localDateTime, DateTimeFormatterUtil.HH_MM_SS_SSSSSS_FMT)); Assert.assertEquals("17:06:30.272150620", DateTimeFormatterUtil.format(localDateTime, DateTimeFormatterUtil.HH_MM_SS_SSSSSSSSS_FMT)); //日期時間格式化 Assert.assertEquals("2020-05-23 17:06:30.272150", DateTimeFormatterUtil.format(localDateTime, DateTimeFormatterUtil.YYYY_MM_DD_HH_MM_SS_SSSSSS_FMT)); Assert.assertEquals("2020-05-23 17:06:30.272150620", DateTimeFormatterUtil.format(localDateTime, DateTimeFormatterUtil.YYYY_MM_DD_HH_MM_SS_SSSSSSSSS_FMT)); //ISO日期時間格式化 Assert.assertEquals("2020-05-23T17:06:30.272150620+0800", DateTimeFormatterUtil.format(localDateTime, DateTimeFormatterUtil.YYYY_MM_DD_T_HH_MM_SS_SSSSSSSSS_Z_FMT)); Assert.assertEquals("2020-05-23T17:06:30.272150620+08:00", DateTimeFormatterUtil.format(localDateTime, DateTimeFormatterUtil.YYYY_MM_DD_T_HH_MM_SS_SSSSSSSSS_XXX_Z_FMT)); }
(2)解析
/** * 含納秒時間格式解析 */ @Test public void parseTimeWithNanoTest(){ LocalDateTime localDateTime1 = DateTimeFormatterUtil.smartParseToLocalDateTime("2020-05-23T17:06:30.272150620+08:00"); LocalDateTime localDateTime2 = DateTimeFormatterUtil.smartParseToLocalDateTime("2020-05-23T17:06:30.272150+08:00"); LocalDateTime localDateTime3 = DateTimeFormatterUtil.smartParseToLocalDateTime("2020-05-23T17:06:30.272+08:00"); Assert.assertTrue(localDateTime3.isBefore(localDateTime2)); Assert.assertTrue(localDateTime2.isBefore(localDateTime1)); }