Java8 Instant时间戳使用小记
Java8Instant时间戳
用于“时间戳”的运算。它是以Unix元年(传统的设定为UTC时区1970年1月1日午夜时分)开始所经历的描述进行运算
1.创建Instant实例,获取系统的当前时间now
/**
*Java8Instant时间戳学习
*/
@Test
publicvoidtestInstant(){
//通过Instant创建Instant实例返回:returnClock.systemUTC().instant();
Instantnow=Instant.now();
//控制台输出:now=2020-12-29T06:32:49.480Z(以ISO-8601格式输出)
System.out.println("now="+now);
}
注意:这里额控制台输出:now=2020-12-29T06:32:49.480Z。
Intance的now方法:
publicstaticInstantnow(){
returnClock.systemUTC().instant();
}
这是输出的世界标准时间,其中T表示时分秒的开始(或者日期与时间的间隔),Z表示这是一个世界标准时间。
Instant是时间戳,是指世界标准时格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数,Instant本身实际上是指明时区了,是0时区(也就是比北京时间少8小时)。
2.获取当前时区的时间(本地时间)
2.1通过方法Instant.now().atZone(ZoneId.systemDefault())获取当前地区的时间
ZonedDateTimezonedDateTime=Instant.now().atZone(ZoneId.systemDefault()); System.out.println(zonedDateTime);
输出结果
2020-12-31T17:31:14.953+08:00[Asia/Shanghai]
2.2通过增加8小时,转化为北京时间
| 方法名称 | 描述 |
|---|---|
| plusMillis() | 增加时间戳时间,以毫秒为单位 |
| minusNanos() | 增加时间戳时间,以纳秒为单位 |
| minusSeconds() | 增加时间戳时间,以秒为单位 |
| TimeUnit.HOURS.toMillis() | 将小时转化为毫秒数 |
//增加8个小时,使Instant.now()返回时间为北京时间
Instantnow2=Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
System.out.println("now2="+now2);
输出结果:now2=2020-12-29T14:35:32.631Z
转换为符合当前的北京时间。
3.通过Instant获取当前时间距离格林威治时间的值
通过getEpochSecond()方法获取距离格林威治时间的秒数
通过toEpochMilli()方法获取距离格林威治时间的毫秒数
//增加8个小时,使Instant.now()返回时间为北京时间
Instantnow2=Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
//获取格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)距离当前时间的秒/毫秒值
System.out.println("距离1970年01月01日00时00分00秒:"+now2.getEpochSecond()+"秒");
System.out.println("距离1970年01月01日00时00分00秒:"+now2.toEpochMilli()+"毫秒");
输出结果:
距离1970年01月01日00时00分00秒:1609435201秒
距离1970年01月01日00时00分00秒:1609435201645毫秒
4.Instant的from、parse方法
4.1java.time.Instant.from(TemporalAccessortemporal)源码:
publicstaticInstantfrom(TemporalAccessortemporal){
if(temporalinstanceofInstant){
return(Instant)temporal;
}
Objects.requireNonNull(temporal,"temporal");
try{
longinstantSecs=temporal.getLong(INSTANT_SECONDS);
intnanoOfSecond=temporal.get(NANO_OF_SECOND);
returnInstant.ofEpochSecond(instantSecs,nanoOfSecond);
}catch(DateTimeExceptionex){
thrownewDateTimeException("UnabletoobtainInstantfromTemporalAccessor:"+
temporal+"oftype"+temporal.getClass().getName(),ex);
}
}
参数:temporal是要转换的时间对象,返回的是一个转换为Instant的瞬间值
如果转换为Instant的时候失败,会抛出异常``DateTimeException`
4.2parse方法源码
publicstaticInstantparse(finalCharSequencetext){
returnDateTimeFormatter.ISO_INSTANT.parse(text,Instant::from);
}
创建自定义的时间戳
//创建自定义的时间戳
System.out.println(Instant.parse("2020-12-29T14:35:32.631Z"));
输出结果
2020-12-29T14:35:32.631Z
5.Instant的其它常用函数
//获取当前时间戳
Instantinstant=Instant.now();
//获得当前时间戳并且增加66毫秒
Instantinstant1=Instant.now().plusMillis(66);
//获得当前时间戳并且减少66毫秒
Instantinstant2=Instant.now().minusMillis(66);
//判断时间戳instant是否在instant1之后,返回boolean
System.out.println(instant.isAfter(instant1));//返回false
//判断时间戳instant是否在instant1之前,返回boolean
System.out.println(instant.isBefore(instant1));//返回true
//判断两个时间戳是否相等,返回boolean值
System.out.println(instant.equals(instant1));//返回false
//获得当前时间戳并增加1小时通过TimeUnit.HOURS.toMillis(1)将小时转换为毫秒,然后通过plusMillis增加
Instantinstant3=Instant.now().plusMillis(TimeUnit.HOURS.toMillis(1));
//获取时间戳instant和instant3相差天数,返回long类型
//如果小于1天,都算零天,大于等于1天,小于2天算一天
System.out.println("相差天数="+instant.until(instant3,ChronoUnit.DAYS));//返回0
//获取时间戳instant和instant3相差的小时数,返回long类型
System.out.println("相差小时="+instant.until(instant3,ChronoUnit.HOURS));//返回1
//获取时间戳instant和instant3相差的毫秒数,返回long类型
System.out.println("相差毫秒数="+instant.until(instant3,ChronoUnit.MILLIS));//返回3600000
输出结果:
false
true
false
相差天数=0
相差小时=1
相差毫秒数=3600000
6.将获取的时间戳转化为LocalDate
Instantnow=Instant.now(); //UTC ZonedDateTimeatZone=now.atZone(ZoneOffset.UTC); //LocalDateTime atZone.toLocalDateTime(); LocalDateTime.from(atZone); //LocalDate atZone.toLocalDate(); LocalDatedate=LocalDate.from(atZone); //LocalDateTime atZone.toLocalDateTime(); LocalDateTime.from(date);
总结
到此这篇关于Java8Instant时间戳使用小记的文章就介绍到这了,更多相关Java8Instant时间戳内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!