Simple to use java8 time package

 import com.sun.org.apache.xml.internal.res.XMLErrorResources_tr;
 4 
 5 import java.text.DateFormat;
 6 import java.text.SimpleDateFormat;
 7 import java.time.*;
 8 import java.time.format.DateTimeFormatter;
 9 import java.time.temporal.ChronoField;
10 import java.time.temporal.ChronoUnit;
11 public class Time {
12     public static void main(String[] args) {
13         //创建时间日期
14         LocalDate date = LocalDate.of(2014, 3, 18);
15         LocalDate today = LocalDate.now();
16         LocalDate date1 = LocalDate.parse("2014-03-18");
17         LocalTime time1 = LocalTime.parse("13:45:20");
18 
19         //获得属性
20         int year = date.get(ChronoField.YEAR);
21         int month = date.get(ChronoField.MONTH_OF_YEAR);
22         int day = date.get(ChronoField.DAY_OF_MONTH);
23 
24         //修改属性
25         date1 = LocalDate.of(2014, 3, 18);
 26 is          the LocalDate DATE2 = date1.withYear (2011 );
 27          the LocalDate date3 = date2.withDayOfMonth (25 );
 28          the LocalDate date4 = date3.with (ChronoField.MONTH_OF_YEAR,. 9 );
 29  
30          // modify attributes 
31 is          date1 = the LocalDate. of (, 2014,. 3, 18 is );
 32          DATE2 date1.plusWeeks = (. 1 );
 33 is          date3 date2.minusYears = (. 3 );
 34 is          date4 date3.plus = (. 6 , ChronoUnit.MONTHS);
 35  
36          // formatted output 
37         System.out.println(date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
38         System.out.println(date.format(DateTimeFormatter.ISO_DATE));
40     }
41 }

 

Guess you like

Origin www.cnblogs.com/lovemeng1314/p/11488227.html