前言
java8的時間日期api給我們提供了極大的便利。如何更好的熟悉使用時間api也是學習java8的一個很重要的知識點,下面我們一起來學習學習。
本篇文章代碼比較多,可以作為工具,需要使用時,再來查閱。
目錄
1.普通Date時間如何轉為LocalDateTime?
以上是從普通的Date對象轉換成java8時間的操作步驟,需要特別注意下時區的問題。
// 設置時區// ZoneId defaultZoneId = ZoneId.systemDefault(); ZoneId defaultZoneId = ZoneId.of("Asia/Shanghai"); System.out.println("set TimeZone : " + defaultZoneId); //以下每個注釋為輸出的結果 //date : Fri Jan 17 16:52:59 CST 2020 Date date = new Date(); System.out.println("date : " + date); //1. instant : 2020-01-17T08:52:59.235Z Instant instant = date.toInstant(); System.out.println("instant : " + instant); //Zone : 默認是UTC+0時區 //2. localDate : 2020-01-17 LocalDate localDate = instant.atZone(defaultZoneId).toLocalDate(); System.out.println("localDate : " + localDate); //3. LocalDateTime: 2020-01-17T16:52:59.235 LocalDateTime localDateTime = instant.atZone(defaultZoneId).toLocalDateTime(); System.out.println("localDateTime : " + localDateTime); //4. ZonedDateTime: 2020-01-17T16:52:59.235+08:00[Asia/Shanghai] ZonedDateTime zonedDateTime = instant.atZone(defaultZoneId); System.out.println("zonedDateTime : " + zonedDateTime);
2. java8時間如何轉換為普通Date?
轉換的過程和上面圖例剛好反過來,代碼如下:
//同樣,我們設置時區為上海 ZoneId defaultZoneId = ZoneId.of("Asia/Shanghai"); System.out.println(" Default TimeZone : " + defaultZoneId); LocalDate localDate = LocalDate.of(2020, 01, 17); Date date = Date.from(localDate.atStartOfDay(defaultZoneId).toInstant()); System.out.println("\n1. LocalDate -> Date"); System.out.println("localDate : " + localDate); System.out.println("date : " + date); LocalDateTime localDateTime = LocalDateTime.of(2020,01,17,17,46,31); Date date2 = Date.from(localDateTime.atZone(defaultZoneId).toInstant()); System.out.println("\n2. LocalDateTime -> Date"); System.out.println("localDateTime : " + localDateTime); System.out.println("date2 : " + date2); ZonedDateTime zonedDateTime = localDateTime.atZone(defaultZoneId); Date date3 = Date.from(zonedDateTime.toInstant()); System.out.println("\n3. ZonedDateTime -> Date"); System.out.println("zonedDateTime : " + zonedDateTime); System.out.println("date3 : " + date3);
3.如何比較時間?
無論是Date,還是java8中的localDate和localDateTime都可以通過compareTo方法來比較時間。SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");Date date1 = sdf.parse("2020-01-17");Date date2 = sdf.parse("2020-01-16");System.out.println("前者如果較大則為正,相等為0,小於為負 : " + date1.compareTo(date2));LocalDate 和LocalDateTime提供了3個方法,isAfter(),isBefore(),isEqual()。
而在java8之前,我們是使用 after(), before(),equals。方法名有所區別,但是功能大同小異。
4.java8中的瞬點Instant
Instant代表一個瞬時的時間點值對象。
它從1970-01-01T00:00:00Z點毫秒計算的,是不可變的,並且是線程安全的。
5.獲取當前時間戳方式?
方式一:new Timestamp(System.currentTimeMillis());
方式二:new Date().getTime();
方式三:Instant。
方式四:Calendar.getInstance()
方式五:LocalDateTime.now()
方式六:LocalDate.now()
Timestamp timestamp = new Timestamp(System.currentTimeMillis());//2020-01-17 17:04:53.346System.out.println(timestamp);//return 1579251953703System.out.println(timestamp.getTime());// 時間戳轉換為 instant 2020-01-17T09:05:53.703ZInstant instant = timestamp.toInstant();System.out.println(instant);//return 1579251953703System.out.println(instant.toEpochMilli());// instant to timestamp 1579251893346Timestamp tsFromInstant = Timestamp.from(instant);System.out.println(tsFromInstant.getTime());
6.時間轉String
時間和字符串的轉換,相信大家都知道一般使用SimpleDateFormat來實現,但是這個api使用不當可能會有線程安全問題,這裡推薦使用如下的方式來做轉換,保證thread-safe。
//使用當前時間測試LocalDateTime now = LocalDateTime.now();System.out.println("Before : " + now);DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");String formatDateTime = now.format(formatter);System.out.println("After : " + formatDateTime);
7.String轉時間
String now = "2020-01-17 17:30";DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");LocalDateTime formatDateTime = LocalDateTime.parse(now, formatter);System.out.println("Before : " + now);System.out.println("After : " + formatDateTime);System.out.println("After : " + formatDateTime.format(formatter));
8.Instant 轉LocalDateTime
Instant instant = Instant.now();System.out.println("Instant : " + instant);// 添加時區為上海LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai"));System.out.println("LocalDateTime : " + ldt);
9.LocalDateTime轉Instant
LocalDateTime dateTime = LocalDateTime.of(2020, 01, 17, 6, 17, 10);//LocalDateTime : 2020-01-17T06:17:10System.out.println("LocalDateTime : " + dateTime);// +hh:mm 如果設置時區為東八區,則要比原來時間晚8小時//Instant : 2020-01-16T22:17:10ZInstant instant = dateTime.toInstant(ZoneOffset.of("+08:00"));System.out.println("Instant : " + instant);
10.java8為什麼出現localDate和localDateTime?
在java8以前,時間是通過Date類等來表示,其中包含了一些比較差的API設計。例如,年份java.util.Date從1900開始,月份從1開始,天從0開始,這很不直觀。第三方日期和時間庫對此進行了一些彌補的流行,例如Joda-Time。
為了解決這些問題並在JDK內核中提供更好的支持,針對Java SE 8設計了一個新的並且沒有這些問題的日期和時間API。
Java SE 8附帶新的日期和時間API,java.time該API 為開發人員提供了大大改善的安全性和功能。新的API很好地建模了領域,並提供了用於對各種開發人員用例進行建模的大量類。
為了方便大家學習討論,我創建了一個java疑難攻堅互助大家庭,和其他傳統的學習交流不同。本群主要致力於解決項目中的疑難問題,在遇到項目難以解決的
問題時,都可以在這個大家庭裡尋求幫助。
公眾號關注 : 【俠夢的開發筆記】後回復【問題的答案】進入:java中Integer包裝類的基本數據類型是?
如果你也經歷過遇到項目難題,無從下手,
他人有可能可以給你提供一些思路和看法,一百個人就有一百種思路,
同樣,如果你也樂於幫助別人,那解決別人遇到的問題,也同樣對你是一種鍛鍊。
自學路上你不孤單,歡迎來公眾號【俠夢的開發筆記】,回復乾貨,為你準備了精選的學習視頻