java8 time包的简单使用

 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         LocalDate date2 = date1.withYear(2011);
27         LocalDate date3 = date2.withDayOfMonth(25);
28         LocalDate date4 = date3.with(ChronoField.MONTH_OF_YEAR, 9);
29 
30         //修改属性
31         date1 = LocalDate.of(2014, 3, 18);
32         date2 = date1.plusWeeks(1);
33         date3 = date2.minusYears(3);
34         date4 = date3.plus(6, ChronoUnit.MONTHS);
35 
36         //格式化输出
37         System.out.println(date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
38         System.out.println(date.format(DateTimeFormatter.ISO_DATE));
40     }
41 }

 

转载于:https://www.cnblogs.com/lovemeng1314/p/11488227.html

你可能感兴趣的:(java)