这里写目录标题
JDK8中的日期和时间的API
一.jdk8之前的API存在着一些问题:
①可变性:像日期和时间这样的类应该是不可变的
②偏移量:Date中的年份是从1990开始的,而月份都是从0开始的
③格式化,格式化只对Date有用,Calendar则不行
二.localDate、localTime、localDateTime方法:
1.now():返回当前的时间
2.of():设置指定的年月日时分秒。没有偏移量
3.getXxx():
4.不可变性:
三.instant类:
1.now()方法:
2.添加时间偏移量
3.toEpochMilli():
获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 ----> Date类中的getTime
4.ofEpochMilli():
通过给定的毫秒数,获取Instance实例 ----> Date(long millis)
四.DateTimeFormatter:
格式化或解析日期、时间。类似于SimpleDateFormat
1.实例化:
有三种方式,这里只写一种;即:自定义格式。
DateTimeFormatter formatter = DateTimeFormatter.ofPartten("yyyy-MM-dd hh:mm:ss");
//格式化
String str = formatter.format(LocalDateTime.now());
System.out.println(str);
//解析:
TemporalAccessor accessor = formatter.parse("2020-6-24 13:13:03");
System.out.println(accessor);