為何Java8需要引入新的日期與時(shí)間庫
1、Date日期輸出可讀性較差
Date date = new Date(); System.out.println(date);
打印輸出的結(jié)果:
Sat Nov 14 11:03:41 CST 2020
2、Date日期的解析、格式化通過JDK自帶的api實(shí)現(xiàn)較為麻煩,通常會使用第三方的日期時(shí)間庫,比如: joda-time , commons-lang
Java8中提供了哪些日期和時(shí)間類
在java.time包中提供了很多新的類,通常主要使用到的是 LocalDate , LocalTime , LocalDateTime , ZoneId , ZoneDateTime ; 關(guān)系圖如下:
LocaDate這個(gè)類本身不包含時(shí)間和時(shí)區(qū)信息,只包含了日期信息;提供了很多方法來獲取常用的值:星期幾,幾月 …
常用的靜態(tài)構(gòu)造 LocaDate 方法
LocalDate.of(2020, 11, 14); //指定年月日 LocalDate.of(2020, Month.NOVEMBER, 14); //指定年月日 使用Month枚舉類 LocalDate.ofYearDay(2020, 10); //2020年第10天 => 2020-01-10 LocalDate.now(); //當(dāng)前時(shí)間 System.out.println(LocalDate.now()); // 比較好的可讀性輸出 => 2020-11-14
LocaDate 常用實(shí)例方法
LocalDate now = LocalDate.of(2020, 11, 14); System.out.println(now.getMonth()); //月份的枚舉 => NOVEMBER System.out.println(now.getMonthValue()); //月份的數(shù)字 => 11 System.out.println(now.getDayOfMonth()); //幾號 => 14 System.out.println(now.getDayOfYear()); // 一年中的第幾天 => 319 System.out.println(now.getDayOfWeek()); // 周幾枚舉 => SATURDAY System.out.println(now.lengthOfMonth()); //本月多少天 => 30 System.out.println(now.lengthOfYear()); //本年多少天 => 366
LocalTime只包含時(shí)間信息
LocalTime.of(12, 9, 10); //時(shí)、分、秒 LocalTime.now(); LocalTime time = LocalTime.of(12, 9, 10); System.out.println(time.getHour()); System.out.println(time.getMinute()); System.out.println(time.getSecond());
LocalDateTime 從這個(gè)類的名字可以看出是合并了 LocalDate , LocalTime ,只包含日期和時(shí)間,不包含時(shí)區(qū)信息
構(gòu)造的方式,可以直接使用靜態(tài)方法創(chuàng)建,也可以通過 LocalDate , LocalTime 合并
LocalDateTime.of(LocalDate.now(), LocalTime.now()); LocalDateTime.of(2020, 11, 14, 13, 10, 50); LocalDate.now().atTime(LocalTime.now()); LocalTime.now().atDate(LocalDate.now()); LocalDateTime.now();
由于 LocalDateTime 是 LocalDate , LocalTime 的合并,所以 LocalDate , LocalTime 有的實(shí)例方法,基本在 LocalDateTime 中都可以找到
ZoneId 用來替代老版本 TimeZone , 每個(gè) ZoneId 都有一個(gè)特定的地區(qū)標(biāo)識;
ZoneId.of("Asia/Shanghai"); ZoneId.systemDefault()
查看所有的地區(qū)標(biāo)識可以進(jìn)入到 ZoneId 源碼
ZoneDateTime帶有日期、時(shí)間、時(shí)區(qū)信息,是 LocalDateTime 和 ZoneId 的組合
ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDateTime.now(), ZoneId.systemDefault()); ZonedDateTime.of(LocalDate.now(),LocalTime.now(),ZoneId.of("Asia/Shanghai"));
經(jīng)常我們會遇到需要求兩個(gè)時(shí)間之間相差的時(shí)間, 如何實(shí)現(xiàn)呢?
Java8也提供給了相應(yīng)的API支持, Duration 、 Period
Duration between = Duration.between(LocalTime.of(13, 0), LocalTime.of(14, 0)); between.getSeconds(); //返回兩個(gè)時(shí)間相差的秒數(shù) => 3600
Duration 是通過秒和毫秒來記錄時(shí)間的長短,所以只能處理兩個(gè) LocalTime , DateLocalTime , ZonedDateTime ; 如果傳入的是 LocalDate ,將會拋出異常
java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds at java.time.LocalDate.until(LocalDate.java:1614) at java.time.Duration.between(Duration.java:475) at com.haixue.crm.stock.service.LocalTest.testDate(LocalTest.java:121) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
在這種情況下就可以使用 Period
Period between1 = Period.between(LocalDate.of(2020, 11, 13), LocalDate.of(2020, 11, 13)); between1.getDays(); //返回相差的天數(shù) => 1
時(shí)間日期的更高級的操作
以為對時(shí)間日期的修改增加減少都是通過第三方依賴包操作,現(xiàn)在原生API已經(jīng)支持
LocalDate now2 = LocalDate.of(2020, 11, 13); System.out.println(now2.plusDays(2)); //加2天 => 2020-11-15 System.out.println(now2.plusMonths(1)); //加1月 => 2020-12-13 System.out.println(now2.plusWeeks(1)); //加一周 => 2020-11-20 System.out.println(now2.minusDays(1)); //減一天 => 2020-11-12 System.out.println(now2.minusMonths(1)); //減一月 => 2020-10-13 System.out.println(now2.minusYears(1)); //減一年 => 2019-11-13 System.out.println(now2.withYear(2021)); //修改年 => 2021-11-13
有時(shí)候我們會遇到需要取本月的最后一天、本月的第一天、調(diào)整日期到下一個(gè)周日… ;這些需求也能夠通過使用 TemporalAdjuster 很好的實(shí)現(xiàn), TemporalAdjuster 能夠?qū)崿F(xiàn)很多定制化的日期操作,Java8在 TemporalAdjusters 已經(jīng)給提供了默認(rèn)的很多實(shí)現(xiàn)。
LocalDate now3 = LocalDate.of(2020, 11, 13); System.out.println(now3.with(TemporalAdjusters.firstDayOfYear())); // 本年的第一天 => 2020-01-01 System.out.println(now3.with(TemporalAdjusters.next(DayOfWeek.MONDAY))); //下一個(gè)周一 => 2020-11-16 System.out.println(now3.with(TemporalAdjusters.lastDayOfMonth())); // 本月的最后一天 => 2020-11-30 System.out.println(now3.with(TemporalAdjusters.lastDayOfYear())); // 本年的最后一天 => 2020-12-31
自定義 TemporalAdjuster 實(shí)現(xiàn)獲取當(dāng)天的開始時(shí)間和當(dāng)天的最后時(shí)間
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 13, 10, 10, 10); System.out.println(localDateTime); System.out.println(localDateTime.with((temporal) -> temporal.with(ChronoField.SECOND_OF_DAY, 0))); // 當(dāng)天的凌晨 => 2020-11-13T00:00 System.out.println(localDateTime.with((temporal) -> temporal.with(ChronoField.SECOND_OF_DAY, temporal.range(ChronoField.SECOND_OF_DAY).getMaximum()))); // 當(dāng)天的最后一刻時(shí)間 => 2020-11-13T23:59:59
解析、格式化
對日期的字符串解析和格式化的操作是常用的,首先看下不用第三方包如何簡單的實(shí)現(xiàn)日期解析
System.out.println(LocalDateTime.parse("2020-11-14T20:50:00")); // 輸出:2020-11-14T20:50 System.out.println(LocalDateTime.parse("2020/11/14 20:50:00", DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"))); // 輸出:2020-11-14T20:50
實(shí)現(xiàn)格式化同樣也簡單
LocalDate now4 = LocalDate.of(2020, 11, 13); System.out.println(now4.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"))); //輸出:2020/11/13 LocalDateTime localDateTime2 = LocalDateTime.of(2020, 11, 13, 10, 10, 10); System.out.println(localDateTime2.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"))); //輸出:2020/11/13 10:10:10
最后一句
到此這篇關(guān)于為何Java8需要引入新的日期與時(shí)間庫的文章就介紹到這了,更多相關(guān)Java8引入新日期與時(shí)間庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java用兩個(gè)例子充分闡述多態(tài)的可拓展性介紹
下面小編就為大家?guī)硪黄猨ava用兩個(gè)例子充分闡述多態(tài)的可拓展性介紹。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06spring cloud config分布式配置中心的高可用問題
本文給大家介紹spring cloud config分布式配置中心的高可用問題,通過整合Eureka來實(shí)現(xiàn)配置中心的高可用,需要的朋友參考下本文2018-01-01安裝多個(gè)jdk導(dǎo)致eclipse打不開問題解決方案
這篇文章主要介紹了安裝多個(gè)jdk導(dǎo)致eclipse打不開問題解決方案,幫助大家更好的理解和使用eclipse,感興趣的朋友可以了解下2020-11-11如何運(yùn)行SpringBoot項(xiàng)目的方法
這篇文章主要介紹了如何運(yùn)行SpringBoot項(xiàng)目的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Java使用quartz實(shí)現(xiàn)定時(shí)任務(wù)示例詳解
這篇文章主要為大家介紹了Java使用quartz實(shí)現(xiàn)定時(shí)任務(wù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Spring Security攔截器引起Java CORS跨域失敗的問題及解決
這篇文章主要介紹了Spring Security攔截器引起Java CORS跨域失敗的問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07JavaWeb開發(fā)之JSTL標(biāo)簽庫的使用、 自定義EL函數(shù)、自定義標(biāo)簽(帶屬性的、帶標(biāo)簽體的)
這篇文章主要介紹了JavaWeb開發(fā)之JSTL標(biāo)簽庫的使用、 自定義EL函數(shù)、自定義標(biāo)簽(帶屬性的、帶標(biāo)簽體的),需要的朋友可以參考下2017-02-02