-
JAVA时间存储类Period和Duration_java中时间
- 网站名称:JAVA时间存储类Period和Duration_java中时间
- 网站分类:技术文章
- 收录时间:2025-09-04 00:25
- 网站地址:
“JAVA时间存储类Period和Duration_java中时间” 网站介绍
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对象使用。
更多相关网站
- JDK8新特性解析:深入比较LocalDateTime和Date之间的区别
- 3s → 30ms!SpringBoot树形结构“开挂”实录:一次查询提速100倍
- Java 8时间类,越用越香_java时间格式类型
- 实现延迟队列,这些你知道吗?_延迟队列最好方案
- SpringBoot示例,第5期:3种方式实现定时任务
- Windows系统安装日期如何修改_win7修改系统安装日期
- SpringBoot扩展——定时任务!_springboot扩展定时任务!怎么解决
- 硬核!最全的延迟任务实现方式汇总!附代码(强烈推荐)
- 解决Snowflake算法时钟回拨的一种方案
- 为什么建议使用 LocalDateTime 而不是 Date
- jdk8Date LocalDateTime类学习笔记
- 侠说java8-LocalDateTime等时间使用手册(全),先mark后看
- LocalDateTime常用方法总结,总有你会用到的
- 最近发表
- 标签列表
-
- mydisktest_v298 (35)
- sql 日期比较 (33)
- document.appendchild (35)
- 头像打包下载 (35)
- 梦幻诛仙表情包 (36)
- java面试宝典2019pdf (26)
- disk++ (30)
- 加密与解密第四版pdf (29)
- iteye (26)
- centos7.4下载 (32)
- intouch2014r2sp1永久授权 (33)
- jdk1.8.0_191下载 (27)
- axure9注册码 (30)
- 兔兔工程量计算软件下载 (27)
- ccproxy破解版 (31)
- aida64模板 (28)
- engine=innodb (33)
- shiro jwt (28)
- segoe ui是什么字体 (27)
- head first java电子版 (32)
- clickhouse中文文档 (28)
- jdk-8u181-linux-x64.tar.gz (32)
- 计算机网络自顶向下pdf (34)
- -dfile.encoding=utf-8 (33)
- jdk1.9下载 (32)