一、新的Date API介绍
LocalDate
LocalTime
Instant
Duration
Period
formater
parsejdk以前的java.util.Date存在的问题
1)比如new Date(119, 2, 18)表示Mon Mar 18 00:00:00 CST 2019,2019年3月18日,year要从1900年加起,month是从0开始,day是从1开始。
2)SimpleDateFormat不是线程安全的,比如多线程情况下simpleDateFormat.parse会出问题。
3)Date名字叫日期,但是后面还有time时间
例子如下:
1 package com.cy.java8; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 7 public class DateTest { 8 9 public static void main(String[] args) throws ParseException { 10 Date date = new Date(119, 2, 18); 11 System.out.println(date); 12 13 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 14 for(int i=0; i<5; i++){ 15 new Thread(()->{ 16 try { 17 Date parseDate = sdf.parse("20190505"); 18 System.out.println(parseDate); 19 } catch (ParseException e) { 20 e.printStackTrace(); 21 } 22 23 }).start(); 24 } 25 } 26 }
二、LocalDate
1 package com.cy.java8; 2 3 import java.time.LocalDate; 4 import java.time.temporal.ChronoField; 5 6 public class DateTest { 7 8 public static void main(String[] args) { 9 testLocalDate(); 10 } 11 12 /** 13 * LocalDate是线程安全的 14 */ 15 private static void testLocalDate(){ 16 LocalDate localDate = LocalDate.of(2019, 10, 2); 17 System.out.println(localDate.getYear()); 18 System.out.println(localDate.getMonth()); 19 System.out.println(localDate.getMonthValue()); 20 System.out.println(localDate.getDayOfYear()); 21 System.out.println(localDate.getDayOfMonth()); 22 System.out.println(localDate.getDayOfWeek()); 23 24 System.out.println(localDate.get(ChronoField.YEAR)); 25 System.out.println(localDate.get(ChronoField.MONTH_OF_YEAR)); 26 System.out.println(localDate.get(ChronoField.DAY_OF_MONTH)); 27 28 LocalDate now = LocalDate.now(); 29 System.out.println(now); 30 } 31 }
console:
2019 OCTOBER 10 275 2 WEDNESDAY 2019 10 2 2019-10-02
三、LocalTime、Instant、Duration、Period简单介绍
1 package com.cy.java8; 2 3 import java.time.*; 4 5 public class DateTest { 6 7 public static void main(String[] args) throws InterruptedException { 8 testLocalTime(); 9 System.out.println("================================="); 10 11 testCombineLocalDateAndTime(); 12 System.out.println("================================="); 13 14 testInstant(); 15 System.out.println("================================="); 16 17 testDuration(); 18 System.out.println("================================="); 19 20 testPeriod(); 21 } 22 23 private static void testLocalTime(){ 24 LocalTime time = LocalTime.now(); 25 System.out.println(time); 26 27 System.out.println(time.getHour()); 28 System.out.println(time.getMinute()); 29 System.out.println(time.getSecond()); 30 } 31 32 private static void testCombineLocalDateAndTime(){ 33 LocalDate localDate = LocalDate.now(); 34 LocalTime time = LocalTime.now(); 35 LocalDateTime localDateTime = LocalDateTime.of(localDate, time); 36 System.out.println(localDateTime); 37 38 LocalDateTime now = LocalDateTime.now(); 39 System.out.println(now); 40 } 41 42 private static void testInstant() throws InterruptedException { 43 Instant start = Instant.now(); 44 Thread.sleep(1000); 45 Instant end = Instant.now(); 46 47 Duration duration = Duration.between(start, end); //类似于之前的System.currentTimeMillis 48 System.out.println(duration.toMillis()); 49 } 50 51 private static void testDuration(){ 52 LocalTime time = LocalTime.now(); 53 LocalTime beforeTime = time.minusHours(1); 54 Duration duration = Duration.between(time, beforeTime); 55 System.out.println(duration.toHours()); 56 } 57 58 private static void testPeriod(){ 59 Period period = Period.between(LocalDate.of(2017, 9, 1), LocalDate.of(2019, 10, 2)); 60 System.out.println(period.getYears()); 61 System.out.println(period.getMonths()); 62 System.out.println(period.getDays()); 63 } 64 }
console:
21:03:28.136 21 3 28 ================================= 2019-10-02T21:03:28.137 2019-10-02T21:03:28.137 ================================= 1001 ================================= -1 ================================= 2 1 1
四、
----