JAVA时间存储类Period和Duration使用详解

 更新时间:2022年09月29日 10:16:03   作者:Java面试365  
这篇文章主要为大家介绍了JAVA时间存储类Period和Duration使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

前言

在JDK1.8中区分了时间和日期的概念,所以有了两个对应的类,LocalDate和LocalTime,区别如下

  • LocalDate表示日期,不会包含具体的时间,例如2022-09-26。
  • LocalTime表示时间,不会包含日期,仅仅包含时分秒。

简单使用如下

LocalDate localDate = LocalDate.now();
// 执行结果:2022-09-26
System.out.println(localDate);
LocalTime localTime = LocalTime.now();
// 执行结果:16:17:12.381
System.out.println(localTime);

区分了时间和日期的概念后,为方便使用JDK1.8又推出了两个用于存储时间和日期的类,分别为Duraction和Period,这两个类的区别和LocalDate和LocalTime是一一对应的。

  • Period:表示日期的存储用于度量年月日,一般用于存储多个日期之间相差的日期值。
  • Duraction:表示时间的存储用于度量时分秒,一般用于存储多个时间之间相差的时间值。

Period类

Period可以应用于存储两个日期之间的日期差,存储年月日。

LocalDate localDate1 = LocalDate.of(2022,8,20);
LocalDate localDate2 = LocalDate.now();
// 计算两个日期之间的差值
Period between = Period.between(localDate1, localDate2);
// 区间是否为0
System.out.println(between.isZero());
// 区间是否为负数
System.out.println(between.isNegative());
// 获取区间相差几年、几月、几日
System.out.println(between.getYears());
System.out.println(between.getMonths());
System.out.println(between.getDays());
// 区间相差的总月份
System.out.println(between.toTotalMonths());

除了主要场景外还提供一些其它API

实例化

// Period对象实例化可以直接调用of方法指定年月日,注意Period只做日期存储所以不会校验年月日是否符合日期规范!!!
Period period = Period.of(2022, 20, 5);
// 所以当我们想取正常日期值时可以调用normalized方法转换
Period period1 = Period.of(2022, 19, 9);
// 展示结果:2022==19
System.out.println(period1.getYears()+"=="+period1.getMonths());
Period period2 = period1.normalized();
// 展示结果:2023==7
System.out.println(period2.getYears()+"=="+period2.getMonths());

设置值

Period period1 = Period.of(2022, 19, 9);
// 值为:9
System.out.println(period1.getDays());
// 修改天数值,不会修改年月
Period period2 = period1.withDays(2);
// 值为:2
System.out.println(period2.getDays());
//=================================================
// plus相加,minus相减
Period period3 = period1.plusDays(2);
// 值为:11
System.out.println(period3.getDays());

年月日都可以用with、plus、minus相关API设置值~

其它API

Period period1 = Period.of(2022, 19, 9);
LocalDate localDate = LocalDate.of(2022,1,2);
// 给指定日期加上Period对象存储的日期
Temporal temporal = period1.addTo(localDate);
// 返回结果:4045-08-11(日期对象)
System.out.println(temporal);
Period period2 = Period.of(1, 1, 2);
// 给指定日期减去Period对象存储的日期
LocalDate localDate1 = LocalDate.of(2022,1,2);
// 返回结果:2020-11-30(日期对象)
System.out.println(period2.subtractFrom(localDate1));

Duraction类

Duraction可以应用于存储两个时间之间的时间差,可以存储时分秒。

LocalTime localTime1 = LocalTime.of(10,1,2);
LocalTime localTime2 = LocalTime.now();
// 得出时间区间间隔的时间差
Duration between = Duration.between(localTime1, localTime2);
// 区间相差天数
System.out.println(between.toDays());
// 区间相差小时
System.out.println(between.toHours());
// 区间相差分钟
System.out.println(between.toMinutes());
// 区间中的秒数
System.out.println(between.getSeconds());
// 区间相差毫秒
System.out.println(between.toMillis());
// 区间相差纳秒
System.out.println(between.toNanos());
System.out.println("======================");
Duration duration = Duration.ZERO;
// 区间是否为0
System.out.println(duration.isZero());
// 区间是否为负
System.out.println(between.isNegative());

其它API

Duration duration = Duration.ofSeconds(10);
// 值为:10
System.out.println(duration.getSeconds());
// 值取反
Duration duration1 = duration.negated();
// 值为:-10
System.out.println(duration1.getSeconds());
// 值除上指定值,得商
Duration duration2 = duration.dividedBy(2);
// 值为:5
System.out.println(duration2.getSeconds());
// 取绝对值
Duration duration3 = Duration.ofSeconds(-9).abs();
// 值为:9
System.out.println(duration3.getSeconds());

Duration对象同样有minus、plus、with相关方法,使用方法可以参考Period对象使用。

以上就是JAVA时间存储类Period和Duration使用详解的详细内容,更多关于JAVA时间存储类Period Duration的资料请关注脚本之家其它相关文章!

相关文章

  • 使用SpringBoot+OkHttp+fastjson实现Github的OAuth第三方登录

    使用SpringBoot+OkHttp+fastjson实现Github的OAuth第三方登录

    这篇文章主要介绍了使用SpringBoot+OkHttp+fastjson实现Github的OAuth第三方登录,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • Java使用selenium爬取b站动态的实现方式

    Java使用selenium爬取b站动态的实现方式

    本文主要介绍了Java使用selenium爬取b站动态的实现方式,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • go语言题解LeetCode88合并两个有序数组示例

    go语言题解LeetCode88合并两个有序数组示例

    这篇文章主要为大家介绍了go语言题解LeetCode88合并两个有序数组示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • java 如何读取远程主机文件

    java 如何读取远程主机文件

    这篇文章主要介绍了java 如何读取远程主机文件的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • IDEA中安装testng插件过程

    IDEA中安装testng插件过程

    这篇文章主要介绍了IDEA中安装testng插件过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • SkyWalking 自定义插件(Spring RabbitMQ)具体分析过程

    SkyWalking 自定义插件(Spring RabbitMQ)具体分析过程

    这篇文章主要介绍了SkyWalking 自定义插件(Spring RabbitMQ)具体分析过程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-02-02
  • Java批量从svn导出多个项目代码实例

    Java批量从svn导出多个项目代码实例

    这篇文章主要介绍了java批量从svn导出多个项目代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • @ComponentScan在spring中无效的原因分析及解决方案

    @ComponentScan在spring中无效的原因分析及解决方案

    这篇文章主要介绍了@ComponentScan在spring中无效的原因分析及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Springboot3.3 整合Cassandra 4.1.5的详细过程

    Springboot3.3 整合Cassandra 4.1.5的详细过程

    这篇文章主要介绍了Springboot3.3 整合Cassandra 4.1.5的详细过程,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2024-06-06
  • Spring事务失效的几种原因

    Spring事务失效的几种原因

    在日常编码过程中常常涉及到事务,在前两天看到一篇文章提到了Spring事务,那么在此总结下在Spring环境下事务失效的几种原因.
    2020-09-09

最新评论