Java 8 Date Time API

Java 8 新特性Java 8 New Features


Java 8 further strengthens the handling of dates and times by releasing the new Date-Time API (JSR 310).

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

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

  • Poor design− The definitions of Java's date/time classes are not consistent. Date classes exist in both the java.util and java.sql packages. Additionally, classes for formatting and parsing are defined in the java.text package. java.util.Date contains both date and time, while java.sql.Date contains only date. Putting it in the java.sql package is unreasonable. Furthermore, these two classes have the same name, which is a very bad design in itself.

  • Troublesome time zone handling− The date classes do not provide internationalization and have no time zone support. Therefore, Java introduced the java.util.Calendar and java.util.TimeZone classes, but they also have all the above problems.

Java 8, in thejava.timepackage, provides many new APIs. The following are two of the more important APIs:

  • Local (local)− Simplifies date-time handling, without the issue of time zones.

  • Zoned (time zone)− Handles date-time with the specified time zone.

The new java.time package covers all operations for handling dates, times, date/time, time zones, instants, process (during), and clock.


Local Date-Time API

The LocalDate/LocalTime and LocalDateTime classes can be used in cases where time zone handling is not necessary. The code is as follows:

Java8Tester.java file

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 hours 15 minutes 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); } }

Executing the above script, the output is:

$ javac Java8Tester.java 
$ java Java8Tester
当前时间: 2016-04-15T16:55:48.668
date1: 2016-04-15
月: APRIL, 日: 15, 秒: 48
date2: 2012-04-10T16:55:48.668
date3: 2014-12-12
date4: 22:15
date5: 20:15:30

Zoned Date-Time API

If we need to take time zones into account, we can use the zoned date-time API:

Java8Tester.java file

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 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); } }

Executing the above script, the output is:

$ javac Java8Tester.java 
$ java Java8Tester
date1: 2015-12-03T10:15:30+08:00[Asia/Shanghai]
ZoneId: Europe/Paris
当期时区: Asia/Shanghai

Java 8 新特性Java 8 New Features

Other extensions