English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/)

Java Reader/Writer

Java Other Topics

Java 8 Date-Time API

Java 8 New Features

Java 8By releasing a new Date-Time API (JSR 310) to further strengthen the handling of dates and times.

In the old version of Java, the date-time API had many problems, including:

  • Not thread-safe − java.util.Date is not thread-safe, and all date classes are mutable, which is one of the biggest problems with Java date classes.

  • Design is very bad − Java's date/The definitions of time classes are not consistent. There are date classes in both the java.util and java.sql packages, as well as classes for formatting and parsing defined in the java.text package. java.util.Date contains both date and time, while java.sql.Date only contains the date. Including it in the java.sql package is not reasonable. Additionally, both of these classes have the same name, which is a very poor design.

  • Time zone handling is麻烦 − The date class does not provide internationalization and has no time zone support. Therefore, Java introduced the java.util.Calendar and java.util.TimeZone classes, but they also have all the aforementioned issues.

Java 8 In java.time The package provides many new APIs. The following are two of the more important APIs:

  • Local (Local) − Simplifies date and time handling without time zone issues.

  • Zoned (Time Zone) − Handles date and time through the specified time zone.

The new java.time package covers all date, time, and date handling./Operations on time, time zones, instants, durations, and clocks.

Localized Date-Time API

LocalDate/The LocalTime and LocalDateTime classes can be used when time zones are not necessary. The code is as follows:

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 time: " + 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 +", second: " + 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 minute
      LocalTime date4 = LocalTime.of(22, 15);
      System.out.println("date4:  \ + date4);
        
      // Parse string
      LocalTime date5 = LocalTime.parse("20:15:30);
      System.out.println("date5:  \ + date5);
   }
}

Execute the above script, the output will be:

$ javac Java8Tester.java 
$ java Java8Tester
Current time: 2016-04-15T16:55:48.668
date1: 2016-04-15
Month: APRIL, day: 15, second: 48
date2: 2012-04-10T16:55:48.668
date3: 2014-12-12
date4: 22:15
date5: 20:15:30

Use the time zone date-time API

If we need to consider time zones, we can use the time zone date-time API:

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("2015-12-03T10:15:30+05:30[Asia/Shanghai]");
      System.out.println("date1:  \ + date1);
        
      ZoneId id = ZoneId.of("Europe/Paris");
      System.out.println("ZoneId:  "); + id);
        
      ZoneId currentZone = ZoneId.systemDefault();
      System.out.println("Current Time Zone:  "); + currentZone);
   }
}

Execute the above script, the output will be:

$ javac Java8Tester.java 
$ java Java8Tester
date1: 2015-12-03T10:15:30+08:00[Asia/Shanghai]
ZoneId:  Europe/Paris
Current Time Zone:  Asia/Shanghai

Java 8 New Features