原文: https://javatutorial.net/java-8-date-time-api

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

    Java 8 日期时间 API - 图1

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

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

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

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

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

    1. import java.time.*;
    2. import java.time.format.DateTimeFormatter;
    3. public class Main {
    4. public static void LocalDateTimeAPI() {
    5. LocalDate date = LocalDate.now();
    6. LocalTime time = LocalTime.now();
    7. LocalDateTime now = LocalDateTime.now();
    8. DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
    9. String formatedTime = now.format(format);
    10. Month month = now.getMonth();
    11. int day = now.getDayOfMonth();
    12. int seconds = now.getSecond();
    13. System.out.println("Current date: " + date);
    14. System.out.println("Current time: " + time);
    15. System.out.println("Current date and time: " + now);
    16. System.out.println("in foramatted manner " + formatedTime);
    17. System.out.println("Month: " + month + "\nDay: " + day + "\nSeconds: " + seconds);
    18. }
    19. public static void main(String[] args) {
    20. LocalDateTimeAPI();
    21. }
    22. }

    输出

    1. Current date: 2019-07-20
    2. Current time: 14:10:58.492
    3. Current date and time: 2019-07-20T14:10:58.492
    4. in foramatted manner 20-07-2019 14:10:58
    5. Month: JULY
    6. Day: 20
    7. Seconds: 58

    时区日期时间 API 示例

    1. import java.time.ZonedDateTime;
    2. import java.time.ZoneId;
    3. public class ZonedTime {
    4. public void testZonedDateTime() {
    5. ZoneId zone = ZoneId.of("Europe/Paris");
    6. System.out.println("ZoneId: " + zone);
    7. ZoneId currentZone = ZoneId.systemDefault();
    8. System.out.println("CurrentZone: " + currentZone);
    9. }
    10. public static void main(String args[]) {
    11. ZonedTime zonedTimeExample = new ZonedTime();
    12. zonedTimeExample.testZonedDateTime();
    13. }
    14. }

    输出

    1. ZoneId: Europe/Paris
    2. CurrentZone: Europe/London

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

    计时单位示例

    1. import java.time.LocalDate;
    2. import java.time.temporal.ChronoUnit;
    3. public class ChronoUnits {
    4. public void chromoUnits() {
    5. LocalDate today = LocalDate.now();
    6. System.out.println("Current date: " + today);
    7. LocalDate week = today.plus(1, ChronoUnit.WEEKS);
    8. System.out.println("1 week from now: " + week);
    9. LocalDate month = today.plus(1, ChronoUnit.MONTHS);
    10. System.out.println("1 month from now: " + month);
    11. LocalDate year = today.plus(1, ChronoUnit.YEARS);
    12. System.out.println("1 year from now: " + year);
    13. LocalDate decade = today.plus(1, ChronoUnit.DECADES);
    14. System.out.println("1 decade from now: " + decade);
    15. }
    16. public static void main(String args[]) {
    17. ChronoUnits ChronoUnitsExample = new ChronoUnits();
    18. ChronoUnitsExample.chromoUnits();
    19. }
    20. }

    输出

    1. Current date: 2019-07-20
    2. 1 week from now: 2019-07-27
    3. 1 month from now: 2019-08-20
    4. 1 year from now: 2020-07-20
    5. 1 decade from now: 2029-07-20

    周期和持续时间

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

    1. import java.time.temporal.ChronoUnit;
    2. import java.time.LocalDate;
    3. import java.time.LocalTime;
    4. import java.time.Duration;
    5. import java.time.Period;
    6. public class PeriodDuration {
    7. public void testDuration() {
    8. LocalTime currentTime = LocalTime.now();
    9. Duration offtime5 = Duration.ofHours(5);
    10. LocalTime timeOff5Hours = currentTime.plus(offtime5);
    11. Duration duration = Duration.between(currentTime, timeOff5Hours);
    12. System.out.println("Duration: " + duration);
    13. }
    14. public void testPeriod() {
    15. LocalDate currentDate = LocalDate.now();
    16. System.out.println("Current date: " + currentDate);
    17. LocalDate nextMonth = currentDate.plus(1, ChronoUnit.MONTHS);
    18. System.out.println("Next month: " + nextMonth);
    19. Period timePeriod = Period.between(nextMonth, currentDate);
    20. System.out.println("Period: " + timePeriod);
    21. }
    22. public static void main(String args[]) {
    23. PeriodDuration periodDuration = new PeriodDuration();
    24. periodDuration.testPeriod();
    25. periodDuration.testDuration();
    26. }
    27. }

    输出

    1. Current date: 2019-07-20
    2. Next month: 2019-08-20
    3. Period: P-1M
    4. Duration: PT-19H