# Java 8 日期时间 API

> 原文： [https://javatutorial.net/java-8-date-time-api](https://javatutorial.net/java-8-date-time-api)

Java 8 引入了新的日期时间 API，其目的是弥补旧的日期时间 API 的缺点。

![java-featured-image](img/e0db051dedc1179e7424b6d998a6a772.jpg)

以前的日期时间 api 不是线程安全的，新日期时间 api 的替代品是它没有任何设置方法。 新 API 修复的另一个缺点是设计不佳。 旧的 API 具有较少直接的日期操作方法。 而且旧 API 的另一个缺点是程序员必须编写大量代码来处理时区问题。

新的 API 不仅解决了所有这些问题，而且还引入了`java.time`包中的 2 个重要类：

*   本地 – 时区处理没有复杂度
*   时区 – 处理各种时区的更复杂的日期时间 API

**本地时间 API** 在不需要时区时应使用

**使用主要方法调用**的本地日期时间 API 的示例

```java
import java.time.*; 
import java.time.format.DateTimeFormatter; 

public class Main { 

    public static void LocalDateTimeAPI() { 

        LocalDate date = LocalDate.now(); 
        LocalTime time = LocalTime.now(); 
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        String formatedTime = now.format(format);      

        Month month = now.getMonth(); 
        int day = now.getDayOfMonth(); 
        int seconds = now.getSecond(); 

        System.out.println("Current date: " + date); 
        System.out.println("Current time: " + time);          
        System.out.println("Current date and time: " + now);         
        System.out.println("in foramatted manner " + formatedTime); 
        System.out.println("Month: " + month + "\nDay: " + day + "\nSeconds: " + seconds);
    } 

    public static void main(String[] args) { 
        LocalDateTimeAPI(); 
    } 
}
```

**输出**：

```java
Current date: 2019-07-20
Current time: 14:10:58.492
Current date and time: 2019-07-20T14:10:58.492
in foramatted manner 20-07-2019 14:10:58
Month: JULY
Day: 20
Seconds: 58
```

**时区日期时间 API 示例**

```java
import java.time.ZonedDateTime;
import java.time.ZoneId;

public class ZonedTime {

    public void testZonedDateTime() {
        ZoneId zone = ZoneId.of("Europe/Paris");
        System.out.println("ZoneId: " + zone);

        ZoneId currentZone = ZoneId.systemDefault();
        System.out.println("CurrentZone: " + currentZone);
    }

   public static void main(String args[]) {
        ZonedTime zonedTimeExample = new ZonedTime();
        zonedTimeExample.testZonedDateTime();
   }
}
```

**输出**

```java
ZoneId: Europe/Paris
CurrentZone: Europe/London
```

如您所见，时区日期时间 API 可以让您访问特定时区，还可以为您提供时区或系统的默认时区。

**计时单位示例**

```java
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class ChronoUnits {

    public void chromoUnits() {
        LocalDate today = LocalDate.now();
        System.out.println("Current date: " + today);

        LocalDate week = today.plus(1, ChronoUnit.WEEKS);
        System.out.println("1 week from now: " + week);

        LocalDate month = today.plus(1, ChronoUnit.MONTHS);
        System.out.println("1 month from now: " + month);

        LocalDate year = today.plus(1, ChronoUnit.YEARS);
        System.out.println("1 year from now: " + year);

        LocalDate decade = today.plus(1, ChronoUnit.DECADES);
        System.out.println("1 decade from now: " + decade);
     }

   public static void main(String args[]) {
        ChronoUnits ChronoUnitsExample = new ChronoUnits();
        ChronoUnitsExample.chromoUnits();
   }
}
```

**输出**：

```java
Current date: 2019-07-20
1 week from now: 2019-07-27
1 month from now: 2019-08-20
1 year from now: 2020-07-20
1 decade from now: 2029-07-20

```

**周期和持续时间**

周期处理基于日期的时间，而持续时间处理基于时间的时间。

```java
import java.time.temporal.ChronoUnit;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Duration;
import java.time.Period;

public class PeriodDuration {
    public void testDuration() {
        LocalTime currentTime = LocalTime.now();
        Duration offtime5 = Duration.ofHours(5);

        LocalTime timeOff5Hours = currentTime.plus(offtime5);
        Duration duration = Duration.between(currentTime, timeOff5Hours);

        System.out.println("Duration: " + duration);
    }

    public void testPeriod() {
        LocalDate currentDate = LocalDate.now();
        System.out.println("Current date: " + currentDate);

        LocalDate nextMonth = currentDate.plus(1, ChronoUnit.MONTHS);
        System.out.println("Next month: " + nextMonth);

        Period timePeriod = Period.between(nextMonth, currentDate);
        System.out.println("Period: " + timePeriod);
    }

   public static void main(String args[]) {
        PeriodDuration periodDuration = new PeriodDuration();
        periodDuration.testPeriod();
        periodDuration.testDuration();
   }
}
```

**输出**

```java
Current date: 2019-07-20
Next month: 2019-08-20
Period: P-1M
Duration: PT-19H
```