一、 Java8之前日期的问题
可变性:日期时间之类的应该是不可变的 偏移性:Date中的年份是从1900年开始的,月份是从0开始的 格式化:格式化只对Date有用,Calendar则不行 线程安全:之前的日期时间不是线程安全的,不能处理闰秒的
在Java8添加了一些新的时间类
LocalTime LocalDate LocalDateTime 这三个类基本的调用方法是一样的,三个类的区别:
LocalTime:本地时间,不包含年月日 LocalDate:本地日期,不包含几点几分几秒 LocalDateTime:本地日期时间,包含年月日、时分秒
二、 LocalDateTime:本地日期时间
因为LocalDate、LocalTime、LocalDateTime三者用法一样,这里只使用LocalDateTime做测试
1. 获取当前日期时间
调用LocalDateTime的的静态方法now(),获取当前的日期/时间/日期和时间;
@Test
public void test ( ) {
LocalDate localDate = LocalDate . now ( ) ;
LocalTime localTime = LocalTime . now ( ) ;
LocalDateTime now = LocalDateTime . now ( ) ;
System . out. println ( localDate) ;
System . out. println ( localTime) ;
System . out. println ( now) ;
}
2021-05-31
11:02:58.919
2021-05-31T11:02:58.919
2. 设置指定的年、月、日、时、分、秒,没有偏移量
调用LocalDateTime的静态方法of()设置指定的日期时间,该方法没有偏移量 。设置时间后并没有改变原时间,而是返回一个LocalDateTime对象
@Test
public void test2 ( ) {
LocalDateTime time = LocalDateTime . of ( 2020 , 5 , 23 , 3 , 15 ) ;
LocalDateTime now = LocalDateTime . now ( ) ;
DateTimeFormatter formatter = DateTimeFormatter . ofPattern ( "yyyy-MM-dd hh:mm:ss" ) ;
String format = formatter. format ( time) ;
String now_time = formatter. format ( now) ;
System . out. println ( now_time) ;
System . out. println ( format) ;
}
// 运行结果: 当前时间并没有改变,这体现了时间的不可变性
2021-05-31 11:30:01
2020-05-23 03:15:00
3. 获取当前时间是今年/这个月/这周的第几天
调用对象的gexxx()方法,获取当前时间对某个时间的相对时间 同Calendar中的get()方法
@Test
public void test3 ( ) {
LocalDateTime now = LocalDateTime . now ( ) ;
System . out. println ( now. getDayOfMonth ( ) ) ;
System . out. println ( now. getDayOfWeek ( ) ) ;
System . out. println ( now. getDayOfYear ( ) ) ;
}
// 运行结果
2021-05-31T11:25:24.646
31
MONDAY
151
4. 返回一个指定的日期
withXXX():返回一个指定的日期,原日期不变。相当于Calendar的set()。不过该方法体现了不可变性:返回值为指定的日期,而真正的日期没有改变
@Test
public void test4 ( ) {
LocalDateTime localDateTime = LocalDateTime . now ( ) ;
LocalDateTime myTime = localDateTime. withDayOfMonth ( 29 ) ;
System . out. println ( "原时间:" + localDateTime ) ;
System . out. println ( "返回值的时间(指定时间):" + myTime) ;
}
// 运行结果
原时间:2021-05-31T11:32:30.723
返回值的时间(指定时间):2021-05-29T11:32:30.723
5.在当前日期时间上加上指定的时间返回一个新的时间,原日期时间不变。相当于Calendar的add()方法
@Test
public void test5 ( ) {
LocalDateTime localDateTime = LocalDateTime . now ( ) ;
LocalDateTime localDateTime2 = localDateTime. plusDays ( 5 ) ;
System . out. println ( "愿时间:" + localDateTime) ;
System . out. println ( "添加后的时间:" + localDateTime2) ;
}
// 运行结果:
原时间:2021-05-31T11:36:05.998
添加后的时间:2021-06-05T11:36:05.998
6.在当前时间上减去指定的时间返回一个新的时间,原日期不变
public void test6 ( ) {
LocalDateTime localDateTime = LocalDateTime . now ( ) ;
LocalDateTime localDateTime3 = localDateTime. minusHours ( 11 ) ;
System . out. println ( "愿时间:" + localDateTime) ;
System . out. println ( "减去后的时间:" + localDateTime3) ;
}
原时间:2021-05-31T11:37:08.265
减去后的时间:2021-05-31T00:37:08.265
三、瞬时:Instant类
Instant:时间线上的一个瞬时点,可能被用来记录应用程序中的事件事件戳 java.time包通过类型Instant提供机器视图,不提供处理人类意义上的时间单位。Instant表示时间线上的一点,不需要任何上下文信息。
1. 实例化对象
通过Instant的静态方法now(),返回默认UTC时区的Instant类的对象
@Test
public void test1 ( ) {
Instant instant = Instant . now ( ) ;
System . out. println ( instant) ;
}
2. 方法
2.1 ofEpocMilli(long epochMilli)
静态方法,返回在1970-1-1 00:00:00基础上加上指定毫秒数之后的Instant类的对象
public void test2 ( ) {
Instant ofEpochMilli = Instant . ofEpochMilli ( 1622344784952L ) ;
System . out. println ( ofEpochMilli) ;
}
2.1 atOffset(ZoneOffset offset)
结合即使的偏移来创建一个OffsetDateTime对象
@Test
public void test1 ( ) {
Instant instant = Instant . now ( ) ;
System . out. println ( instant) ;
OffsetDateTime myTime = instant. atOffset ( ZoneOffset . ofHours ( 8 ) ) ;
System . out. println ( myTime) ;
}
// 运行结果
2021-05-31T04:00:03.794Z
2021-05-31T12:00:03.794+08:00
2.1 toEpochMilli()
返回1970-1-1 00:00:00到当前时间的毫秒数,即为时间戳
@Test
public void test3 ( ) {
Instant instant = Instant . now ( ) ;
long time = instant. toEpochMilli ( ) ;
System . out. println ( time) ;
}
四、DateTimeFormatter类
该类同SimpleDateFormat类相似 该类提供了三种格式化方法:
主要方法:
ofPattern(String pattern):静态方法:返回一个指定字符串格式的DateTimeFormatter format(TemporalAccessor t):格式化一个日期、时间,返回字符串 parse(CharSequence text):将指定格式的字符序列解析为一个日期时间
预定义的标准格式
预定义格式作为DateTimeFormatter的静态成员变量被调用
@Test
public void test1 ( ) {
DateTimeFormatter formatter = DateTimeFormatter . ISO_DATE_TIME;
LocalDateTime localDateTime = LocalDateTime . now ( ) ;
String format = formatter. format ( localDateTime) ;
System . out. println ( "localDateTime = " + localDateTime) ;
System . out. println ( "format = " + format) ;
TemporalAccessor parse = formatter. parse ( format) ;
System . out. println ( parse) ;
}
本地化相关格式
@Test
public void test2 ( ) {
LocalDateTime localDateTime = LocalDateTime . now ( ) ;
DateTimeFormatter formatter = DateTimeFormatter . ofLocalizedDateTime ( FormatStyle . LONG) ;
String format = formatter. format ( localDateTime) ;
System . out. println ( format) ;
TemporalAccessor parse = formatter. parse ( format) ;
System . out. println ( parse) ;
}
自定义格式
@Test
public void test3 ( ) {
String pattern = "yyyy-MM-dd hh:mm:ss" ;
DateTimeFormatter formatter = DateTimeFormatter . ofPattern ( pattern) ;
LocalDateTime localDateTime = LocalDateTime . now ( ) ;
String format = formatter. format ( localDateTime) ;
System . out. println ( "format = " + format) ;
TemporalAccessor parse = formatter. parse ( format) ;
System . out. println ( "parse = " + parse) ;
}