Java8的常用時(shí)間api實(shí)用指南
前言
Java 8 提供了一套新的時(shí)間 api ,比之前的 Calendar 類要簡(jiǎn)單明了很多。常用的有三個(gè)類 Instant、LocalDate 、LocalDateTime , Instant 是用來表示時(shí)刻的,類似 Unix 的時(shí)間,表示從協(xié)調(diào)世界時(shí)1970年1月1日0時(shí)0分0秒起至現(xiàn)在的總秒數(shù),也可以獲取毫秒。LocalDate 表示一個(gè)日期,只有年月日,沒有時(shí)分秒。LocalDateTime 就是年月日時(shí)分秒了。
下面話不多說了,來一起看看詳細(xì)的介紹吧
Instant
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println("Now secoonds:" + now.getEpochSecond());
System.out.println("Now milli :" + now.toEpochMilli());
}
輸出當(dāng)前時(shí)刻距離 1970年1月1日0時(shí)0分0秒 的秒和毫秒
Now secoonds:1541321299
Now milli :1541321299037
LocalDateTime
為了方便輸出時(shí)間格式,Java8 提供了 DateTimeFormatter 類來替代之前的 SimpleDateFormat。
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println("Now: " + now.format(formatter));
}
Now: 2018-11-04 16:53:09
LocalDateTime 提供了很多時(shí)間計(jì)算的方法,比如 加一個(gè)小時(shí),減去一周,加上一天等等這樣的計(jì)算,比之前的 Calendar 要方便許多。
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println("Now: " + now.format(formatter));
LocalDateTime nowPlusDay = now.plusDays(1);
System.out.println("Now + 1 day: " + nowPlusDay.format(formatter));
LocalDateTime nowMinusHours = now.minusHours(5);
System.out.println("Now - 5 hours: " + nowMinusHours.format(formatter));
}
Now: 2018-11-04 17:02:53
Now + 1 day: 2018-11-05 17:02:53
Now - 5 hours: 2018-11-04 12:02:53
LocalDateTime 還有 isAfter 、 isBefore 和 isEqual 方法可以用來比較兩個(gè)時(shí)間。LocalDate 的用法和 LocalDateTime 是類似的。
Instant 和 LocalDateTime 的互相轉(zhuǎn)換
這倆的互相轉(zhuǎn)換都要涉及到一個(gè)時(shí)區(qū)的問題。LocalDateTime 用的是系統(tǒng)默認(rèn)時(shí)區(qū)。我們可以先把 LocalDateTime 轉(zhuǎn)為 ZonedDateTime ,然后再轉(zhuǎn)成 Instant。
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println("Now: " + now.format(formatter));
Instant nowInstant = now.atZone(ZoneId.systemDefault()).toInstant();
System.out.println("Now mini seconds: " + nowInstant.toEpochMilli());
}
Now: 2018-11-04 17:19:16
Now mini seconds: 1541323156101
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Instant now = Instant.now();
System.out.println("Now mini seconds: " + now.toEpochMilli());
LocalDateTime nowDateTime = LocalDateTime.ofInstant(now, ZoneId.systemDefault());
System.out.println("Zone id: " + ZoneId.systemDefault().toString());
System.out.println("Now: " + nowDateTime.format(formatter));
}
Now mini seconds: 1541323844781
Zone id: Asia/Shanghai
Now: 2018-11-04 17:30:44
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
SpringBoot通過整合Dubbo解決@Reference注解問題
這篇文章主要介紹了SpringBoot通過整合Dubbo解決@Reference注解問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java計(jì)算兩個(gè)漢字相似度的實(shí)現(xiàn)方法
有時(shí)候我們希望計(jì)算兩個(gè)漢字的相似度,比如文本的 OCR 等場(chǎng)景,用于識(shí)別糾正,本文給大家詳細(xì)介紹了Java計(jì)算兩個(gè)漢字相似度的實(shí)現(xiàn)方法,文中有詳細(xì)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-11-11
java 矩陣乘法的mapreduce程序?qū)崿F(xiàn)
這篇文章主要介紹了java 矩陣乘法的mapreduce程序?qū)崿F(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-06-06

