New Date API Introduction

First, the introduction of new Date API

LOCALDATE
localtime
Instant
Duration
Period

formater
existing parsejdk previous question java.util.Date

1) For example, new Date (119, 2, 18) represents Mon Mar 18 00:00:00 CST 2019, 2019 Nian 3 Yue 18 Ri, year from 1900 onwards plus, month starting from 0, day starting from 1 .

2) SimpleDateFormat is not thread-safe, such as multiple threads simpleDateFormat.parse go wrong.

3) Date named date, but after that there are time time

 

Examples are as follows:

 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 }

 

Two, 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

 

three, 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

----

Guess you like

Origin www.cnblogs.com/tenWood/p/11618269.html
Recommended