龙空技术网

JAVA时间存储类Period和Duration

Java面试365 159

前言:

当前兄弟们对“java365”大概比较关注,同学们都需要分析一些“java365”的相关文章。那么小编在网摘上汇集了一些关于“java365””的相关知识,希望小伙伴们能喜欢,同学们一起来学习一下吧!

JAVA时间存储类Period和Duration前言

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

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

简单使用如下

LocalDate localDate = LocalDate.now();// 执行结果:2022-09-26System.out.println(localDate);LocalTime localTime = LocalTime.now();// 执行结果:16:17:12.381System.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);// 区间是否为0System.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==19System.out.println(period1.getYears()+"=="+period1.getMonths());Period period2 = period1.normalized();// 展示结果:2023==7System.out.println(period2.getYears()+"=="+period2.getMonths());
设置值
Period period1 = Period.of(2022, 19, 9);// 值为:9System.out.println(period1.getDays());// 修改天数值,不会修改年月Period period2 = period1.withDays(2);// 值为:2System.out.println(period2.getDays());//=================================================// plus相加,minus相减Period period3 = period1.plusDays(2);// 值为:11System.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;// 区间是否为0System.out.println(duration.isZero());// 区间是否为负System.out.println(between.isNegative());
其它API
Duration duration = Duration.ofSeconds(10);// 值为:10System.out.println(duration.getSeconds());// 值取反Duration duration1 = duration.negated();// 值为:-10System.out.println(duration1.getSeconds());// 值除上指定值,得商Duration duration2 = duration.dividedBy(2);// 值为:5System.out.println(duration2.getSeconds());// 取绝对值Duration duration3 = Duration.ofSeconds(-9).abs();// 值为:9System.out.println(duration3.getSeconds());

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

标签: #java365