iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >JAVA时间存储类Period和Duration使用详解
  • 716
分享到

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

2024-04-02 19:04:59 716人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录前言Period类实例化设置值其它apiDuraction类其它API前言 在jdk1.8中区分了时间和日期的概念,所以有了两个对应的类,LocalDate和LocalTime,

前言

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的资料请关注编程网其它相关文章!

--结束END--

本文标题: JAVA时间存储类Period和Duration使用详解

本文链接: https://www.lsjlt.com/news/168668.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作