Java 8 - New Date/Time API
1
Java 8-概述
2
Java 8 - Lambda表達(dá)式
3
Java 8 - 方法引用
4
Java8-Functional Interfaces函數(shù)式接口
5
Java 8 - 默認(rèn)方法
6
Java 8 - Streams
7
Java 8 - Optional Class可選類
8
Java 8 - Nashorn JavaScript
9
Java 8 - New Date/Time API
使用Java 8重窟,引入了一個(gè)新的Date-Time API來(lái)解決舊的日期時(shí)間API的以下缺點(diǎn) -
原文鏈接:http://blogxinxiucan.sh1.newtouch.com/2017/07/19/Java-8-New-Date-Time-API/
- 線程不安全 - java.util.Date不是線程安全的讥邻,因此開(kāi)發(fā)人員必須在使用日期時(shí)處理并發(fā)問(wèn)題。新的日期時(shí)間API是不可變的顽馋,沒(méi)有setter方法。
- 設(shè)計(jì)差 - 默認(rèn)日期從1900開(kāi)始,月份從1開(kāi)始,日期從0開(kāi)始罕邀,所以沒(méi)有一致性。舊的API對(duì)日期操作的方法較少抓督。新的API為此類操作提供了許多實(shí)用方法燃少。
- 困難的時(shí)區(qū)處理 - 開(kāi)發(fā)人員不得不編寫大量代碼來(lái)處理時(shí)區(qū)問(wèn)題束亏。新的API已經(jīng)開(kāi)發(fā)出了保持領(lǐng)域特定的設(shè)計(jì)铃在。
Java 8在java.time包下引入了一個(gè)新的日期時(shí)間API 。以下是java.time包中介紹的一些重要類 -
- 本地 - 簡(jiǎn)化的日期時(shí)間API碍遍,沒(méi)有時(shí)區(qū)處理的復(fù)雜性定铜。
- 分區(qū) - 處理各種時(shí)區(qū)的專用日期API。
本地日期時(shí)間API
LocalDate / LocalTime和LocalDateTime類簡(jiǎn)化了不需要時(shí)區(qū)的開(kāi)發(fā)怕敬。讓我們看看他們?cè)谛袆?dòng) -
Java8Tester.java
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.Month;
public class Java8Tester {
public static void main(String args[]){
Java8Tester java8tester = new Java8Tester();
java8tester.testLocalDateTime();
}
public void testLocalDateTime(){
// Get the current date and time
LocalDateTime currentTime = LocalDateTime.now();
System.out.println("Current DateTime: " + currentTime);
LocalDate date1 = currentTime.toLocalDate();
System.out.println("date1: " + date1);
Month month = currentTime.getMonth();
int day = currentTime.getDayOfMonth();
int seconds = currentTime.getSecond();
System.out.println("Month: " + month +"day: " + day +"seconds: " + seconds);
LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
System.out.println("date2: " + date2);
//12 december 2014
LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 12);
System.out.println("date3: " + date3);
//22 hour 15 minutes
LocalTime date4 = LocalTime.of(22, 15);
System.out.println("date4: " + date4);
//parse a string
LocalTime date5 = LocalTime.parse("20:15:30");
System.out.println("date5: " + date5);
}
}
驗(yàn)證結(jié)果
使用javac編譯器編譯類揣炕,如下所示:
$javac Java8Tester.java
現(xiàn)在運(yùn)行Java8Tester如下 -
$java Java8Tester
它應(yīng)該產(chǎn)生以下輸出 -
Current DateTime: 2014-12-09T11:00:45.457
date1: 2014-12-09
Month: DECEMBERday: 9seconds: 45
date2: 2012-12-10T11:00:45.457
date3: 2014-12-12
date4: 22:15
date5: 20:15:30
分區(qū)日期時(shí)間API
在考慮時(shí)區(qū)時(shí)使用分區(qū)日期時(shí)間API。讓我們看看他們?cè)谛袆?dòng) -
Java8Tester.java
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class Java8Tester {
public static void main(String args[]){
Java8Tester java8tester = new Java8Tester();
java8tester.testZonedDateTime();
}
public void testZonedDateTime(){
// Get the current date and time
ZonedDateTime date1 = ZonedDateTime.parse("2007-12-03T10:15:30+05:30[Asia/Karachi]");
System.out.println("date1: " + date1);
ZoneId id = ZoneId.of("Europe/Paris");
System.out.println("ZoneId: " + id);
ZoneId currentZone = ZoneId.systemDefault();
System.out.println("CurrentZone: " + currentZone);
}
}
驗(yàn)證結(jié)果
使用javac編譯器編譯類东跪,如下所示:
$javac Java8Tester.java
現(xiàn)在運(yùn)行Java8Tester如下 -
$java Java8Tester
它應(yīng)該產(chǎn)生以下輸出 -
date1: 2007-12-03T10:15:30+05:00[Asia/Karachi]
ZoneId: Europe/Paris
CurrentZone: Etc/UTC
計(jì)時(shí)單位枚舉
java.time.temporal.ChronoUnit
枚舉在Java 8中添加畸陡,以替換舊API中使用的整數(shù)值來(lái)表示日,月等虽填。讓我們看看它們?cè)谛袆?dòng)中 -
Java8Tester.java
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Java8Tester {
public static void main(String args[]){
Java8Tester java8tester = new Java8Tester();
java8tester.testChromoUnits();
}
public void testChromoUnits(){
//Get the current date
LocalDate today = LocalDate.now();
System.out.println("Current date: " + today);
//add 1 week to the current date
LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
System.out.println("Next week: " + nextWeek);
//add 1 month to the current date
LocalDate nextMonth = today.plus(1, ChronoUnit.MONTHS);
System.out.println("Next month: " + nextMonth);
//add 1 year to the current date
LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
System.out.println("Next year: " + nextYear);
//add 10 years to the current date
LocalDate nextDecade = today.plus(1, ChronoUnit.DECADES);
System.out.println("Date after ten year: " + nextDecade);
}
}
驗(yàn)證結(jié)果
使用javac編譯器編譯類丁恭,如下所示:
$javac Java8Tester.java
現(xiàn)在運(yùn)行Java8Tester如下 -
$java Java8Tester
應(yīng)該產(chǎn)生以下結(jié)果 -
Current date: 2014-12-10
Next week: 2014-12-17
Next month: 2015-01-10
Next year: 2015-12-10
Date after ten year: 2024-12-10
期間與期限
使用Java 8,引入了兩個(gè)專門的課程來(lái)處理時(shí)間差異 -
- 期間 - 它處理基于日期的時(shí)間量斋日。
- 持續(xù)時(shí)間 - 它處理時(shí)間的時(shí)間量
牲览。
讓我們以一個(gè)例子來(lái)理解他們 -
Java8Tester.java
import java.time.temporal.ChronoUnit;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Duration;
import java.time.Period;
public class Java8Tester {
public static void main(String args[]){
Java8Tester java8tester = new Java8Tester();
java8tester.testPeriod();
java8tester.testDuration();
}
public void testPeriod(){
//Get the current date
LocalDate date1 = LocalDate.now();
System.out.println("Current date: " + date1);
//add 1 month to the current date
LocalDate date2 = date1.plus(1, ChronoUnit.MONTHS);
System.out.println("Next month: " + date2);
Period period = Period.between(date2, date1);
System.out.println("Period: " + period);
}
public void testDuration(){
LocalTime time1 = LocalTime.now();
Duration twoHours = Duration.ofHours(2);
LocalTime time2 = time1.plus(twoHours);
Duration duration = Duration.between(time1, time2);
System.out.println("Duration: " + duration);
}
}
驗(yàn)證結(jié)果
使用javac編譯器編譯類,如下所示:
$javac Java8Tester.java
現(xiàn)在運(yùn)行Java8Tester如下 -
$java Java8Tester
它應(yīng)該產(chǎn)生以下輸出 -
Current date: 2014-12-10
Next month: 2015-01-10
Period: P-1M
Duration: PT2H
時(shí)間調(diào)整器
TemporalAdjuster用于執(zhí)行日期數(shù)學(xué)恶守。例如第献,獲得“本月第二個(gè)星期六”或“下周二”贡必。讓我們看一個(gè)例子 -
Java8Tester.java
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.time.DayOfWeek;
public class Java8Tester {
public static void main(String args[]){
Java8Tester java8tester = new Java8Tester();
java8tester.testAdjusters();
}
public void testAdjusters(){
//Get the current date
LocalDate date1 = LocalDate.now();
System.out.println("Current date: " + date1);
//get the next tuesday
LocalDate nextTuesday = date1.with(TemporalAdjusters.next(DayOfWeek.TUESDAY));
System.out.println("Next Tuesday on : " + nextTuesday);
//get the second saturday of next month
LocalDate firstInYear = LocalDate.of(date1.getYear(),date1.getMonth(), 1);
LocalDate secondSaturday = firstInYear.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY)).with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
System.out.println("Second Saturday on : " + secondSaturday);
}
}
驗(yàn)證結(jié)果
使用javac編譯器編譯類,如下所示:
$javac Java8Tester.java
現(xiàn)在運(yùn)行Java8Tester如下 -
$java Java8Tester
應(yīng)該產(chǎn)生以下結(jié)果 -
Current date: 2014-12-10
Next Tuesday on : 2014-12-16
Second Saturday on : 2014-12-13
向后兼容性
將一個(gè)toInstant()方法添加到原始的Date和Calendar對(duì)象中庸毫,可以將它們轉(zhuǎn)換為新的Date-Time API仔拟。使用一個(gè)Instant(Insant,ZoneId)方法來(lái)獲取一個(gè)LocalDateTime或ZonedDateTime對(duì)象岔绸。讓我們以一個(gè)例子來(lái)理解它 -
Java8Tester.java
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Date;
import java.time.Instant;
import java.time.ZoneId;
public class Java8Tester {
public static void main(String args[]){
Java8Tester java8tester = new Java8Tester();
java8tester.testBackwardCompatability();
}
public void testBackwardCompatability(){
//Get the current date
Date currentDate = new Date();
System.out.println("Current date: " + currentDate);
//Get the instant of current date in terms of milliseconds
Instant now = currentDate.toInstant();
ZoneId currentZone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(now, currentZone);
System.out.println("Local date: " + localDateTime);
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, currentZone);
System.out.println("Zoned date: " + zonedDateTime);
}
}
驗(yàn)證結(jié)果
使用javac編譯器編譯類理逊,如下所示:
$javac Java8Tester.java
現(xiàn)在運(yùn)行Java8Tester如下 -
$java Java8Tester
它應(yīng)該產(chǎn)生以下輸出 -
Current date: Wed Dec 10 05:44:06 UTC 2014
Local date: 2014-12-10T05:44:06.635
Zoned date: 2014-12-10T05:44:06.635Z[Etc/UTC]