Latest web development tutorials

Java 8 Date Time API

Java 8 new features Java 8 new features


Nashorn a javascript engine.

Java 8 by issuing new Date-Time API (JSR 310) to further strengthen the date and time of processing.

In older versions of Java, there is a date and time API problems, including:

  • Non-thread-safe - java.util.Date not thread-safe, all class dates are variable, which is one of the biggest problems Java Date class.

  • Poor design - Define Java date / time classes are not consistent in the java.util and java.sql package has a date of classes, in addition to formatting and parsing class definition in the java.text package.java.util.Date include both the date and time, and java.sql.Date only contains the date, which was included in the java.sql package and unreasonable. In addition these two classes have the same name, which itself is a very bad design.

  • Timezone handling trouble - Date class does not offer international, no time zone support, Java and java.util.TimeZone introduced java.util.Calendar class, but they exist all the same problems as described above.

Java 8 injava.time package provides many new API.The following are two of the more important API:

  • Local (Local) - simplifies the handling of date and time, no problem time zone.

  • Zoned (time zone) - Processing date and time by time zone enacted.

New java.time package covers all processing date, time, date / time, time zone, time (instants), process (during) and clock (clock) operation.


Localized date and time API

LocalDate / LocalTime and LocalDateTime class area is not necessary when dealing with the case. 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(){
	
      // 获取当前的日期时间
      LocalDateTime currentTime = LocalDateTime.now();
      System.out.println("当前时间: " + 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 +", 日: " + day +", 秒: " + 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 小时 15 分钟
      LocalTime date4 = LocalTime.of(22, 15);
      System.out.println("date4: " + date4);
		
      // 解析字符串
      LocalTime date5 = LocalTime.parse("20:15:30");
      System.out.println("date5: " + date5);
   }
}

Implementation of the above script, 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

Datetime API time zone

If we need to take into account the time zone, you can use the date and time API time zone:

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(){
	
      // 获取当前时间日期
      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("当期时区: " + currentZone);
   }
}

Implementation of the above script, output is:

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

Java 8 new features Java 8 new features