您的位置 首页 java

JAVA时间存储类Period和Duration

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

文章地址:https://www.zhihuclub.com/172960.shtml

关于作者: 智云科技

热门文章

网站地图