java8 的 Period 和Duration 介绍

本文章主要介绍一下 period 以及duration 的主要方法与使用功能

Period 类

可以根据年、月、日来模拟一个数量或者时间量的工具类。使用between()方法可以获取两个日期之间的差作为Period对象返回

 public static void main(String[] args) {LocalDate startTime = LocalDate.of(2020,01,30);LocalDate endTime = LocalDate.of(2021,04,8);Period period = Period.between(startTime,endTime);log.info("两个时间之间的差值  年:{},月:{},日:{}",period.getYears(),period.getMonths(),period.getDays());}

执行代码可知,我们使用Period.between()方法计算出了两个之间年月日差值,接下来我们在看看Period类还有那些方法

1.of和get

使用of可自定义日期量,使用get方法可获取对应的日期单位量,没有设置的日期默认都为0

public static void main(String[] args) {Period period = Period.of(2021,04,8);Period periodYear = Period.ofYears(2021);Period periodMonths = Period.ofMonths(04);Period periodDays = Period.ofDays(8);log.info("给period赋予的值:年:{},月:{},日:{}",period.getYears(),period.getMonths(),period.getDays());log.info("给periodYear赋予的值:年:{},月:{},日:{}",periodYear.getYears(),periodYear.getMonths(),periodYear.getDays());log.info("给periodMonths赋予的值:年:{},月:{},日:{}",periodMonths.getYears(),periodMonths.getMonths(),periodMonths.getDays());log.info("给periodDays赋予的值:年:{},月:{},日:{}",periodDays.getYears(),periodDays.getMonths(),periodDays.getDays());log.info("使用get取值{}",period.get(ChronoUnit.YEARS));}执行结果-----
14:16:44.512 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 给period赋予的值:年:2021,月:4,日:8
14:16:44.515 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 给periodYear赋予的值:年:2021,月:0,日:0
14:16:44.515 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 给periodMonths赋予的值:年:0,月:4,日:0
14:16:44.515 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 给periodDays赋予的值:年:0,月:0,日:8
14:16:44.516 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 使用get取值2021

在使用period.get()方法时,我们看到入参使用了ChronoUnit枚举类

2.ChronoUnit 类

用于选择时间的单位

public enum ChronoUnit implements TemporalUnit {/*** 纳秒概念的单位,最小支持的时间单位。对于ISO日历系统,它等于第二个单位的第1,000,000,000个部分。*/NANOS("Nanos", Duration.ofNanos(1)),/*** 微秒概念的单位。对于ISO日历系统,它等于第二个单位的百万分之一。*/MICROS("Micros", Duration.ofNanos(1000)),/*** 代表毫秒概念的单位。对于ISO日历系统,它等于第二个单位的千分之一。*/MILLIS("Millis", Duration.ofNanos(1000_000)),/*** 代表秒的概念的单位。对于ISO日历系统,除around秒外,它等于SI单位制中的秒。*/SECONDS("Seconds", Duration.ofSeconds(1)),/*** 代表分钟概念的单位。对于ISO日历系统,它等于60秒。*/MINUTES("Minutes", Duration.ofSeconds(60)),/*** 代表小时概念的单位。对于ISO日历系统,它等于60分钟。*/HOURS("Hours", Duration.ofSeconds(3600)),/** 代表半天概念的单位,用于AM / PM。对于ISO日历系统,它等于12小时。*/HALF_DAYS("HalfDays", Duration.ofSeconds(43200)),/*** 代表一天的概念的单位。对于ISO日历系统,这是从午夜到午夜的标准日期。估计一天的持续时间为24小时。
与其他日历系统一起使用时,它必须与地球上太阳升起和落下所定义的日期相对应。不必将日期从午夜开始-在日历系统之间进行转换时,日期应等于午夜。*/DAYS("Days", Duration.ofSeconds(86400)),/*** 代表一周概念的单位。对于ISO日历系统,它等于7天。
与其他日历系统一起使用时,它必须对应整数天*/WEEKS("Weeks", Duration.ofSeconds(7 * 86400L)),/*** 代表一个月概念的单位。对于ISO日历系统,月份的长度因一年中的月份而异。估计的持续时间为365.2425天的十二分之一。
与其他日历系统一起使用时,它必须对应整数天。*/MONTHS("Months", Duration.ofSeconds(31556952L / 12)),/*** 代表一年概念的单位。对于ISO日历系统,它等于12个月。一年的估计持续时间为365.2425天。
当与其他日历系统一起使用时,它必须对应于整数天或月,大约等于地球绕太阳公转所定义的一年。*/YEARS("Years", Duration.ofSeconds(31556952L)),/*** 代表十年概念的单位。对于ISO日历系统,它等于10年。
与其他日历系统一起使用时,它必须对应整数天,通常是整数年。*/DECADES("Decades", Duration.ofSeconds(31556952L * 10L)),/*** 代表一个世纪概念的单位。对于ISO日历系统,它等于100年。
与其他日历系统一起使用时,它必须对应整数天,通常是整数年。*/CENTURIES("Centuries", Duration.ofSeconds(31556952L * 100L)),/*** 代表千年概念的单位。对于ISO日历系统,它等于1000年。
与其他日历系统一起使用时,它必须对应整数天,通常是整数年。*/MILLENNIA("Millennia", Duration.ofSeconds(31556952L * 1000L)),/*** 代表时代概念的单位。 ISO日历系统没有纪元,因此无法在日期或日期时间中添加纪元。人为地将时代的估计持续时间定义为1,000,000,000年。
与其他日历系统一起使用时,该装置没有任何限制。*/ERAS("Eras", Duration.ofSeconds(31556952L * 1000_000_000L)),/*** 代表永恒概念的人造单位。这主要与TemporalField一起使用,以表示无限制的字段,例如年份或时代。人为地将时代的估计持续时间定义为“持续时间”支持的最大持续时间。*/FOREVER("Forever", Duration.ofSeconds(Long.MAX_VALUE, 999_999_999));}

2.isNegative

检查此时间段的三个单位中是否有一个为负数。
这将检查年,月或天的单位是否小于零。
返回值:
如果此期间的任何单位为负,则为true

    public static void main(String[] args) {Period period = Period.of(2021,-1,8);log.info("校验年月日任何一位值是否有负数:{}",period.isNegative());}15:12:06.981 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 校验年月日任何一位值是否有负数:true

3.parse

从特定格式的字符串中解析时间,格式为(PnYnMnD or PnW)字符主要要包含四个部分并且按顺序出现,这些部分为年,月,周和日的ASCII后缀“Y”,“M”,“W”,"D"大小写都可以,任何基于周输入的都将乘以7,并是为天数

    public static void main(String[] args) {Period period = Period.parse("P2021Y04M8D");Period period2 = Period.parse("P2021Y04M1W");log.info("period 日期格式:年:{},月:{},日:{}",period.getYears(),period.getMonths(),period.getDays());log.info("period2 日期格式:年:{},月:{},日:{}",period2.getYears(),period2.getMonths(),period2.getDays());}10:50:30.194 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - period 日期格式:年:2021,月:4,日:8
10:50:30.197 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - period2 日期格式:年:2021,月:4,日:7Process finished with exit code 0

plusX()、minuX() 方法进行增加或减少,这里的X只具体的Years、Months、Days

    public static void main(String[] args) {Period period = Period.parse("P2021Y04M8D");log.info("原来的 period 日期格式:年:{},月:{},日:{}",period.getYears(),period.getMonths(),period.getDays());log.info("使用plus后period 日期格式:年:{},月:{},日:{}",period.plusYears(1).getYears(),period.plusMonths(2).getMonths(),period.plusDays(3).getDays());log.info("使用minus后period 日期格式:年:{},月:{},日:{}",period.minusYears(1).getYears(),period.minusMonths(2).getMonths(),period.minusDays(3).getDays());}16:20:44.177 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 原来的 period 日期格式:年:2021,月:4,日:8
16:20:44.180 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 使用plus后period 日期格式:年:2022,月:6,日:11
16:20:44.180 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 使用minus后period 日期格式:年:2020,月:2,日:5Process finished with exit code 0

Duration 类

duration 此类以秒合纳秒未单位对时间量进行建模,在上个类的基础可更加精密准确的处理时间

between、toDays、toHours、toMinutes、getSeconds、toMillis、toNanos的使用

    public static void main(String[] args) {Instant start = Instant.parse("2021-04-13T13:52:30.00Z");Instant end = Instant.parse("2021-04-14T10:16:30.00Z");Duration duration = Duration.between(start, end);log.info("start 和 end 之间的时差值天:{},小时:{},分钟:{},秒:{},毫秒:{},纳秒:{}",duration.toDays(),duration.toHours(),duration.toMinutes(),duration.getSeconds(),duration.toMillis(),duration.toNanos());}14:11:30.402 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - start 和 end 之间的时差值天:0,小时:20,分钟:1224,秒:73440,毫秒:73440000,纳秒:73440000000000Process finished with exit code 0

isNegative()

用于校验前面的时间是否大于前面的实际

    public static void main(String[] args) {Instant start = Instant.parse("2021-04-13T13:52:30.00Z");Instant end = Instant.parse("2021-04-14T10:16:30.00Z");Duration duration = Duration.between(end,start);log.info("end 是否大于 start :{}",duration.isNegative());Duration duration2 = Duration.between(start,end);log.info("start 是否大于 end :{}",duration2.isNegative());}14:15:43.360 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - end 是否大于 start :true
14:15:43.364 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - start 是否大于 end :falseProcess finished with exit code 0

介绍Duration 的常用方法 如parse()、plus()、minus()等

    public static void main(String[] args) {Duration duration = Duration.parse("P1DT5H20M34.42S");log.info("转换的时间为  天:{}、小时:{}、分钟:{}、秒:{}、毫秒:{}、纳秒:{}",duration.toDays(),duration.toHours(),duration.toMinutes(),duration.getSeconds(),duration.toMillis(),duration.toNanos());Duration duration2 = duration.parse("P1DT-5H");log.info("转换的时间为  天:{}、小时:{}、分钟:{}、秒:{}、毫秒:{}、纳秒:{}",duration2.toDays(),duration2.toHours(),duration2.toMinutes(),duration2.getSeconds(),duration2.toMillis(),duration2.toNanos());/* 单独对天、时、分、秒、毫秒、纳秒 增加或减 */log.info("添加指定时间后的结果为   天:{}、小时:{}、分钟:{}、秒:{}、毫秒:{}、纳秒:{}",duration2.plus(1,ChronoUnit.DAYS).toDays(),duration2.plus(6,ChronoUnit.HOURS).toHours(),duration2.plus(5,ChronoUnit.MINUTES).toMinutes(),duration2.plus(10,ChronoUnit.SECONDS).getSeconds(),duration2.plus(10,ChronoUnit.MILLIS).toMillis(),duration2.plus(5,ChronoUnit.NANOS).toNanos());log.info("减去指定时间后的结果为   天:{}、小时:{}、分钟:{}、秒:{}、毫秒:{}、纳秒:{}",duration2.minusDays(1).toDays(),duration2.minusHours(6).toHours(),duration2.minusMinutes(5).toMinutes(),duration2.minusSeconds(10).getSeconds(),duration2.minusMillis(10).toMillis(),duration2.minusNanos(5).toNanos());/* 使用Duration.ofX()创建指定时间的Duration对象 */Duration days = Duration.ofDays(2);Duration hours = Duration.ofHours(1);Duration minutes = Duration.ofMinutes(50);Duration seconds = Duration.ofSeconds(5000);Duration millis = Duration.ofMillis(50000);Duration nanos = Duration.ofNanos(600000);log.info("天:{},小时:{},分钟:{},秒:{},毫秒:{},纳秒:{}",days.toDays(),hours.toHours(),minutes.toMinutes(),seconds.getSeconds(),millis.toMillis(),nanos.toNanos());/* 使用Duration.toX()转换时间格式 */log.info("分钟:{},秒:{}",hours.toMinutes(),minutes.getSeconds());}15:13:19.748 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 转换的时间为  天:1、小时:29、分钟:1760、秒:105634、毫秒:105634420、纳秒:105634420000000
15:13:19.756 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 转换的时间为  天:0、小时:19、分钟:1140、秒:68400、毫秒:68400000、纳秒:68400000000000
15:13:19.756 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 添加指定时间后的结果为   天:1、小时:25、分钟:1145、秒:68410、毫秒:68400010、纳秒:68400000000005
15:13:19.756 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 减去指定时间后的结果为   天:0、小时:13、分钟:1135、秒:68390、毫秒:68399990、纳秒:68399999999995
15:13:19.756 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 天:2,小时:1,分钟:50,秒:5000,毫秒:50000,纳秒:600000
15:13:19.757 [main] INFO com.gafc.lk.advice.GlobalExceptionHandler - 分钟:60,秒:3000Process finished with exit code 0

总结

本文我们介绍了Java8 中 Period 和Duration类,并通过实例介绍了其常用方法
本文借鉴了 https://blog.csdn.net/neweastsun/article/details/88770592 博客,在此特别感谢

Period 、Duration常用方法使用与介绍相关推荐

  1. Requests库常用方法及参数介绍

    Requests库常用方法及参数介绍 Requests 库中定义了七个常用的请求方法,这些方法各自有着不同的作用,在这些请求方法中 requests.get() 与 requests.post() 方 ...

  2. StringUtils常用方法+StringUtils详细介绍

    StringUtils用法+StringUtils详细介绍 博文来源:http://yijianfengvip.blog.163.com/blog/static/1752734322012122219 ...

  3. JDK 1.8中Instant时间戳类,Duration类,Period类,等一些与事件相关的类和接口

    Instant时间戳类 (一)含义:从1970-01-01 00:00:00到当前时间的毫秒值 (二)常用的方法:  now(): 获取当前的时间,获取的是当前的美国时间,和处于东八区的我们相差八个小 ...

  4. java时间单位框架接口TemporalAmount及其实现类Duration和Period

    上一篇我们聊了时间单位抽象出的接口TemporalUnit,今天我们来聊时间段抽象出的接口TemporalAmount及其实现类Duration和Period. 实例化emporalAmount的实现 ...

  5. MPEG-DASH介绍

    1. MPEG-DASH介绍 前言:从接触MPEG-DASH到现在大概也有3个多月了,前几天需要在课上总结下这半年的工作,大概做了一个ppt,现在想进一步总结下,写一个关于DASH入门的文章,供后人参 ...

  6. DASH标准ABR算法介绍

    文章目录 1. What is DASH? 1.1 MPD文件 1.2 QoE & ABR算法 附:VBR & AVC/H.264编码 附:DASH白皮书结构 [2.c] 2. Why ...

  7. java8新特性Lambda和Stream以及函数式接口等新特性介绍

    主要内容 1.Lambda 表达式 2.函数式接口 3.方法引用与构造器引用 4.Stream API 5.接口中的默认方法与静态方法 6.新时间日期API 7.其他新特性 Java 8新特性简介 速 ...

  8. StringUtils工具类的常用方法

    StringUtils 方法的操作对象是 java.lang.String 类型的对象,是对 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String  ...

  9. Java Stream(流)的分类, 四大基本流的介绍

    上一篇文章已经介绍过什么是流, 以及流的基本概念 http://blog.csdn.net/nvd11/article/details/29917065 本文主要介绍java四大基本流的方法. 一, ...

  10. ue4场景没阴影_UE4性能调试分析常用方法

    UE4性能调试分析常用方法 本文将介绍使用UE4开发项目常用到的性能分析的命令 UE4使用命令的方式是在游戏运行的时候按~键就可以输入命令了 显示CPU和GPU端执行耗时命令: stat UNIT 这 ...

最新文章

  1. ologit模型与logit_Stata-多元 Logit 模型详解 (mlogit)
  2. 智办事:高效远程办公指南
  3. java基础之面试篇四-单向链表和双向链表区别
  4. iOS进阶之底层原理-isa与对象
  5. 在Python中执行while循环
  6. Android开发笔记(二十三)文件对话框FileDialog
  7. vue 移动端选择器
  8. ZK 在XML编辑器中设置.zul文件
  9. JQuery slidebox实现图片轮播
  10. select_related和prefetch_related的用法与区别
  11. 转载:SyncToy安装使用详解
  12. CE6 CPU 使用率
  13. ubuntu16.04安装onlyoffice
  14. 信道编码算法的发展和应用
  15. python画图:小圆覆盖大圆问题
  16. Python+Vue计算机毕业设计美食网站设计与实现62e76(源码+程序+LW+部署)
  17. 多孔氮化硼你了解吗?官能化/修饰/掺杂多孔氮化硼纳米纤维。分享介绍
  18. 产品经理必备的五款办公软件
  19. canvas封闭图形填充与
  20. 工业互联网将给中国制造业怎样的未来?

热门文章

  1. 原生js生成气泡碰撞,随机生成颜色
  2. 微信小程序滑动验证拼图(有效果图)
  3. python去马赛克
  4. 动态规划经典例题——最长公共子序列和最长公共子串(python)
  5. 获取input输入值和获取form表单中的组件输入值区别
  6. Java练习用Java写双人游戏简单四子棋
  7. php unpack 详解,【PHP】 pack unpack 详解
  8. 把一个人的特点写具体作文_把一个人的特点写具体
  9. 自然语言处理(NLP):08 TextCNN在短文本分类应用介绍
  10. 攻防世界PWN新手练习区WP