JDK8中新日期时间API

  1 package day2_14;
  2 
  3 import org.junit.Test;
  4 
  5 import java.time.*;
  6 import java.time.format.DateTimeFormatter;
  7 import java.time.format.FormatStyle;
  8 import java.time.temporal.TemporalAccessor;
  9 
 10 /**
 11  * @Author Tianhao
 12  * @create 2021-02-14-21:47
 13  */
 14 public class JDK8DateTimeTest {
 15 
 16     /*
 17     LocalDate、LocalTime、LocalDateTime 的使用
 18     说明:
 19          1.LocalDateTime用的多一些
 20          2.类似于Calendar
 21      */
 22 
 23     @Test
 24     public void test1() {
 25         //两种实例化方式
 26 
 27         //第一种:now():获取当前日期、时间、或者日期+时间来创建对象
 28         LocalDate localDate = LocalDate.now();
 29         LocalTime localTime = LocalTime.now();
 30         LocalDateTime localDateTime = LocalDateTime.now();
 31         System.out.println(localDate);//2021-02-14
 32         System.out.println(localTime);//22:11:01.820
 33         System.out.println(localDateTime);//2021-02-14T22:11:01.820
 34 
 35         //第二种:of():设置指定的年、月、日、时、分、秒。没有偏移量
 36         LocalDateTime localDateTime1 = LocalDateTime.of(2021, 2, 18, 13, 14, 59);
 37         System.out.println(localDateTime1);//2021-02-18T13:14:59
 38 
 39         System.out.println("**************");
 40         //getXxx()  获得相关的属性
 41         System.out.println(localDateTime.getDayOfMonth());
 42         System.out.println(localDateTime.getDayOfWeek()); //SUNDAY
 43         System.out.println(localDateTime.getDayOfYear());
 44         System.out.println(localDateTime.getMinute());
 45         System.out.println(localDateTime.getMonth());//FEBRUARY
 46         System.out.println(localDateTime.getMonthValue());//2
 47 
 48         System.out.println("****************");
 49         //下面都体现不可变性
 50         //withXxx()  设置相关的属性
 51         LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(28);
 52         System.out.println(localDateTime);//2021-02-14T22:13:00.105
 53         System.out.println(localDateTime2);//2021-02-28T22:13:00.105
 54 
 55         LocalDateTime localDateTime3 = localDateTime.withHour(4);
 56         System.out.println(localDateTime);//2021-02-14T22:16:14.357
 57         System.out.println(localDateTime3);//2021-02-14T04:16:14.357
 58 
 59         System.out.println("****************");
 60         //增加相关属性值
 61         LocalDateTime localDateTime4 = localDateTime.plusMonths(3);
 62         System.out.println(localDateTime);//2021-02-14T22:19:52.258
 63         System.out.println(localDateTime4);//2021-05-14T22:19:52.258
 64 
 65         //减少相关属性值
 66         LocalDateTime localDateTime5 = localDateTime.minusDays(6);
 67         System.out.println(localDateTime);//2021-02-14T22:22:18.514
 68         System.out.println(localDateTime5);//2021-02-08T22:22:18.514
 69     }
 70 
 71 
 72     /*
 73         Instant:瞬时类
 74         类似于java.util.Date类
 75      */
 76 
 77     @Test
 78     public void test2() {
 79         //创建对象方式一:
 80         //now():获取本初子午线对应的标准时间
 81         Instant instant = Instant.now();
 82         System.out.println(instant);//2021-02-14T14:39:03.994Z
 83 
 84         //中国在东八区,时钟需要加上8小时的偏移量
 85         OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
 86         System.out.println(offsetDateTime);//2021-02-14T22:39:03.994+08:00
 87 
 88         //toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数
 89         //类似于Date类的getTime()
 90         long milli = instant.toEpochMilli();
 91         System.out.println(milli);//1613313872550
 92 
 93         System.out.println("***************");
 94         //创建对象方式二:
 95         //ofEpochMilli():通过给定的毫秒数获取一个Instant实例
 96         //类似于Date(Long millis)构造器
 97         Instant instant1 = Instant.ofEpochMilli(1613313872550L);
 98         System.out.println(instant1);//2021-02-14T14:44:32.550Z
 99     }
100 
101     /*
102         DateTimeFormatter:格式化或解析日期、时间
103         类似于SimpleDateFormat
104          */
105     @Test
106     public void test3 () {
107 
108     //3种实例化方式
109        //方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
110         DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
111         LocalDateTime localDateTime = LocalDateTime.now();
112         //格式化:日期--->字符串
113         String str = formatter.format(localDateTime);
114         System.out.println(localDateTime);//2021-02-15T13:08:53.245  --LocalDateTime类型
115         System.out.println(str);//2021-02-15T13:08:53.245  --String类型
116 
117         //解析:字符串-->日期
118         //TemporalAccessor接口,这里是多态
119         TemporalAccessor parse = formatter.parse("2021-02-15T13:08:53.245");
120         System.out.println(parse);//{},ISO resolved to 2021-02-15T13:08:53.245
121 
122         System.out.println("***************");
123         //方式二:本地化相关的格式。如:ofLocalizedDateTime(FormatStyle.LONG)
124         DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
125         String str2 = formatter2.format(localDateTime);
126         System.out.println(str2);//2021年2月15日 下午01时21分21秒
127         TemporalAccessor parse2 = formatter2.parse("2021年2月14日 下午03时59分59秒");
128         System.out.println(parse2);//
129 
130         System.out.println("***************");
131         //重点:方式三:自定义的格式,如:ofPattern("yyyy-MM-dd hh-mm-ss")
132         DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
133         //格式化
134         String str3 = formatter3.format(localDateTime);
135         System.out.println(str3);//2021-02-15 01:36:11
136         //解析
137         TemporalAccessor parse3 = formatter3.parse("2021-02-15 02:59:59");
138         System.out.println(parse3);
139         //{NanoOfSecond=0, MicroOfSecond=0, HourOfAmPm=2, MinuteOfHour=59, MilliOfSecond=0, SecondOfMinute=59},ISO resolved to 2021-02-15
140 
141 
142     }
143 }

 

上一篇:JDK8的LocalDateTime用法


下一篇:JDK8日期API