詳解Java8 新特性之日期API
Java 8 在包java.time下包含了一組全新的時間日期API。下面的例子展示了這組新API里最重要的一些部分:
1.Clock 時鐘
Clock類提供了訪問當前日期和時間的方法,Clock是時區(qū)敏感的,可以用來取代 System.currentTimeMillis() 來獲取當前的微秒數(shù)。某一個特定的時間點也可以使用Instant類來表示,Instant類也可以用來創(chuàng)建老的java.util.Date對象。
Clock clock = Clock.systemDefaultZone(); long millis = clock.millis(); Instant instant = clock.instant(); Date legacyDate = Date.from(instant); // legacy java.util.Date
2.Timezones 時區(qū)
在新API中時區(qū)使用ZoneId來表示。時區(qū)可以很方便的使用靜態(tài)方法of來獲取到。 時區(qū)定義了到UTS時間的時間差,在Instant時間點對象到本地日期對象之間轉(zhuǎn)換的時候是極其重要的。
System.out.println(ZoneId.getAvailableZoneIds()); // prints all available timezone ids ZoneId zone1 = ZoneId.of("Europe/Berlin"); ZoneId zone2 = ZoneId.of("Brazil/East"); System.out.println(zone1.getRules()); System.out.println(zone2.getRules()); // ZoneRules[currentStandardOffset=+01:00] // ZoneRules[currentStandardOffset=-03:00]
3.LocalTime 本地時間
LocalTime 定義了一個沒有時區(qū)信息的時間,例如 晚上10點,或者 17:30:15。下面的例子使用前面代碼創(chuàng)建的時區(qū)創(chuàng)建了兩個本地時間。之后比較時間并以小時和分鐘為單位計算兩個時間的時間差:
LocalTime now1 = LocalTime.now(zone1); LocalTime now2 = LocalTime.now(zone2); System.out.println(now1.isBefore(now2)); // false long hoursBetween = ChronoUnit.HOURS.between(now1, now2); long minutesBetween = ChronoUnit.MINUTES.between(now1, now2); System.out.println(hoursBetween); // -3 System.out.println(minutesBetween); // -239
LocalTime 提供了多種工廠方法來簡化對象的創(chuàng)建,包括解析時間字符串。
LocalTime localTime= LocalTime.of(23, 59, 59); System.out.println(localTime); // 23:59:59 DateTimeFormatter germanFormatter = DateTimeFormatter .ofLocalizedTime(FormatStyle.SHORT) .withLocale(Locale.GERMAN); LocalTime leetTime = localTime.format(germanFormatter); System.out.println(leetTime);
4.LocalDate 本地日期
LocalDate 表示了一個確切的日期,比如 2014-03-11。該對象值是不可變的,用起來和LocalTime基本一致。下面的例子展示了如何給Date對象加減天/月/年。另外要注意的是這些對象是不可變的,操作返回的總是一個新實例。
LocalDate today = LocalDate.now(); LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS); LocalDate yesterday = tomorrow.minusDays(2); LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4); DayOfWeek dayOfWeek = independenceDay.getDayOfWeek(); System.out.println(dayOfWeek); // FRIDAY
從字符串解析一個LocalDate類型和解析LocalTime一樣簡單:
DateTimeFormatter germanFormatter = DateTimeFormatter .ofLocalizedDate(FormatStyle.MEDIUM) .withLocale(Locale.GERMAN); LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter); System.out.println(xmas); // 2014-12-24
5.LocalDateTime 本地日期時間
LocalDateTime 同時表示了時間和日期,相當于前兩節(jié)內(nèi)容合并到一個對象上了。LocalDateTime和LocalTime還有LocalDate一樣,都是不可變的。LocalDateTime提供了一些能訪問具體字段的方法。
LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59); DayOfWeek dayOfWeek = sylvester.getDayOfWeek(); System.out.println(dayOfWeek); // WEDNESDAY Month month = sylvester.getMonth(); System.out.println(month); // DECEMBER long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY); System.out.println(minuteOfDay); // 1439
只要附加上時區(qū)信息,就可以將其轉(zhuǎn)換為一個時間點Instant對象,Instant時間點對象可以很容易的轉(zhuǎn)換為老式的java.util.Date。
Instant instant = sylvester .atZone(ZoneId.systemDefault()) .toInstant(); Date legacyDate = Date.from(instant); System.out.println(legacyDate); // Wed Dec 31 23:59:59 CET 2014
格式化LocalDateTime和格式化時間和日期一樣的,除了使用預定義好的格式外,我們也可以自己定義格式:
DateTimeFormatter formatter = DateTimeFormatter .ofPattern("MMM dd, yyyy - HH:mm"); LocalDateTime parsed = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter); String string = formatter.format(parsed); System.out.println(string); // Nov 03, 2014 - 07:13
和java.text.NumberFormat不一樣的是新版的DateTimeFormatter是不可變的,所以它是線程安全的。
以上所述是小編給大家介紹的Java8 新特性之日期API,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
spring?@value無法取值多個properties文件的解決
這篇文章主要介紹了spring?@value無法取值多個properties文件的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03idea手動執(zhí)行maven命令的三種實現(xiàn)方式
這篇文章主要介紹了idea手動執(zhí)行maven命令的三種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08springboot如何獲取applicationContext?servletContext
這篇文章主要介紹了springboot如何獲取applicationContext?servletContext問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01Spring接口版本控制方案及RequestMappingHandlerMapping接口介紹(最新推薦)
RequestMappingHandlerMapping接口是Spring MVC中的一個核心組件,負責處理請求映射和處理器的匹配這篇文章主要介紹了Spring接口版本控制方案及RequestMappingHandlerMapping接口介紹,需要的朋友可以參考下2024-07-07