Java日期时间API系列17-----Jdk8中java.time包中的新的日期时间API类,java日期计算4,2个日期对比,获取相差年月日部分属性和相差总的天时分秒毫秒纳秒等
通过Java日期时间API系列9-----Jdk8中java.time包中的新的日期时间API类的Period和Duration的区别 ,可以看出java8设计非常好,新增了Period和Duration类,专用于对比2个时间场景:
Period,可以获取2个时间相差的年月日的属性。
Duration,可以获取2个时间相差总的天时分秒毫秒纳秒。
下面应用:
- /**
- * 获取2个日期的相差年月天的年数部分
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenYears(LocalDateTime startInclusive, LocalDateTime endExclusive){
- Objects.requireNonNull(startInclusive, "startInclusive");
- Objects.requireNonNull(endExclusive, "endExclusive");
- return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
- DateTimeConverterUtil.toLocalDate(endExclusive)).getYears();
- }
- /**
- * 获取2个日期的相差年月天的年数部分
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenYears(Date startInclusive, Date endExclusive){
- Objects.requireNonNull(startInclusive, "startInclusive");
- Objects.requireNonNull(endExclusive, "endExclusive");
- return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
- DateTimeConverterUtil.toLocalDate(endExclusive)).getYears();
- }
- /**
- * 获取2个日期的相差年月天的年数部分
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenYears(LocalDate startInclusive, LocalDate endExclusive){
- return Period.between(startInclusive, endExclusive).getYears();
- }
- /**
- * 获取2个日期的相差年月天的月数部分
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenMonths(LocalDateTime startInclusive, LocalDateTime endExclusive){
- Objects.requireNonNull(startInclusive, "startInclusive");
- Objects.requireNonNull(endExclusive, "endExclusive");
- return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
- DateTimeConverterUtil.toLocalDate(endExclusive)).getMonths();
- }
- /**
- * 获取2个日期的相差年月天的月数部分
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenMonths(Date startInclusive, Date endExclusive){
- Objects.requireNonNull(startInclusive, "startInclusive");
- Objects.requireNonNull(endExclusive, "endExclusive");
- return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
- DateTimeConverterUtil.toLocalDate(endExclusive)).getMonths();
- }
- /**
- * 获取2个日期的相差年月天的月数部分
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenMonths(LocalDate startInclusive, LocalDate endExclusive){
- return Period.between(startInclusive, endExclusive).getMonths();
- }
- /**
- * 获取2个日期的相差年月天的天数部分
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenDays(LocalDateTime startInclusive, LocalDateTime endExclusive){
- Objects.requireNonNull(startInclusive, "startInclusive");
- Objects.requireNonNull(endExclusive, "endExclusive");
- return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
- DateTimeConverterUtil.toLocalDate(endExclusive)).getDays();
- }
- /**
- * 获取2个日期的相差年月天的天数部分
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenDays(Date startInclusive, Date endExclusive){
- Objects.requireNonNull(startInclusive, "startInclusive");
- Objects.requireNonNull(endExclusive, "endExclusive");
- return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
- DateTimeConverterUtil.toLocalDate(endExclusive)).getDays();
- }
- /**
- * 获取2个日期的相差年月天的天数部分
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenDays(LocalDate startInclusive, LocalDate endExclusive){
- return Period.between(startInclusive, endExclusive).getDays();
- }
- /**
- * 获取2个日期的相差总天数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalDays(LocalDateTime startInclusive, LocalDateTime endExclusive){
- return Duration.between(startInclusive, endExclusive).toDays();
- }
- /**
- * 获取2个日期的相差总天数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalDays(Date startInclusive, Date endExclusive){
- Objects.requireNonNull(startInclusive, "startInclusive");
- Objects.requireNonNull(endExclusive, "endExclusive");
- return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toDays();
- }
- /**
- * 获取2个日期的相差总小时数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalHours(LocalDateTime startInclusive, LocalDateTime endExclusive){
- return Duration.between(startInclusive, endExclusive).toHours();
- }
- /**
- * 获取2个日期的相差总小时数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalHours(LocalTime startInclusive, LocalTime endExclusive){
- return Duration.between(startInclusive, endExclusive).toHours();
- }
- /**
- * 获取2个日期的相差总小时数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalHours(Date startInclusive, Date endExclusive){
- Objects.requireNonNull(startInclusive, "startInclusive");
- Objects.requireNonNull(endExclusive, "endExclusive");
- return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toHours();
- }
- /**
- * 获取2个日期的相差总分钟数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalMinutes(LocalDateTime startInclusive, LocalDateTime endExclusive){
- return Duration.between(startInclusive, endExclusive).toMinutes();
- }
- /**
- * 获取2个日期的相差总分钟数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalMinutes(LocalTime startInclusive, LocalTime endExclusive){
- return Duration.between(startInclusive, endExclusive).toMinutes();
- }
- /**
- * 获取2个日期的相差总分钟数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalMinutes(Date startInclusive, Date endExclusive){
- Objects.requireNonNull(startInclusive, "startInclusive");
- Objects.requireNonNull(endExclusive, "endExclusive");
- return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toMinutes();
- }
- /**
- * 获取2个日期的相差总秒数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalSeconds(LocalDateTime startInclusive, LocalDateTime endExclusive){
- return Duration.between(startInclusive, endExclusive).getSeconds();
- }
- /**
- * 获取2个日期的相差总秒数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalSeconds(LocalTime startInclusive, LocalTime endExclusive){
- return Duration.between(startInclusive, endExclusive).getSeconds();
- }
- /**
- * 获取2个日期的相差总秒数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalSeconds(Date startInclusive, Date endExclusive){
- Objects.requireNonNull(startInclusive, "startInclusive");
- Objects.requireNonNull(endExclusive, "endExclusive");
- return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).getSeconds();
- }
- /**
- * 获取2个日期的相差总毫秒数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalMillis(LocalDateTime startInclusive, LocalDateTime endExclusive){
- return Duration.between(startInclusive, endExclusive).toMillis();
- }
- /**
- * 获取2个日期的相差总毫秒数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalMillis(LocalTime startInclusive, LocalTime endExclusive){
- return Duration.between(startInclusive, endExclusive).toMillis();
- }
- /**
- * 获取2个日期的相差总毫秒数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalMillis(Date startInclusive, Date endExclusive){
- Objects.requireNonNull(startInclusive, "startInclusive");
- Objects.requireNonNull(endExclusive, "endExclusive");
- return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toMillis();
- }
- /**
- * 获取2个日期的相差总纳秒数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalNanos(LocalDateTime startInclusive, LocalDateTime endExclusive){
- return Duration.between(startInclusive, endExclusive).toNanos();
- }
- /**
- * 获取2个日期的相差总纳秒数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalNanos(LocalTime startInclusive, LocalTime endExclusive){
- return Duration.between(startInclusive, endExclusive).toNanos();
- }
- /**
- * 获取2个日期的相差总纳秒数
- * @param startInclusive
- * @param endExclusive
- * @return
- */
- public static long betweenTotalNanos(Date startInclusive, Date endExclusive){
- Objects.requireNonNull(startInclusive, "startInclusive");
- Objects.requireNonNull(endExclusive, "endExclusive");
- return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toNanos();
- }
测试代码
- /**
- * 使用Period比较2个LocalDate
- */
- @Test
- public void dateCalculatorPeriodBetweenTest(){
- LocalDate localDate = LocalDate.now();
- LocalDate localDate2 = LocalDate.of(2021, 3, 7);
- System.out.println(localDate);
- System.out.println(localDate2);
- System.out.println(DateTimeCalculatorUtil.betweenYears(localDate, localDate2));
- System.out.println(DateTimeCalculatorUtil.betweenMonths(localDate, localDate2));
- System.out.println(DateTimeCalculatorUtil.betweenDays(localDate, localDate2));
- }
- /**
- * 使用Period比较2个Date
- */
- @Test
- public void dateCalculatorPeriodBetweenTest2(){
- Date date = new Date();
- LocalDate localDate2 = LocalDate.of(2021, 3, 7);
- Date date2 = DateTimeConverterUtil.toDate(localDate2);
- System.out.println(date);
- System.out.println(date2);
- System.out.println(DateTimeCalculatorUtil.betweenYears(date, date2));
- System.out.println(DateTimeCalculatorUtil.betweenMonths(date, date2));
- System.out.println(DateTimeCalculatorUtil.betweenDays(date, date2));
- }
- /**
- * 使用Duration比较2个LocalDateTime
- */
- @Test
- public void dateCalculatorDurationBetweenTest(){
- LocalDateTime localDateTime = LocalDateTime.now();
- LocalDateTime localDateTime2 = LocalDateTime.of(2021, 3, 7, 22, 10, 10);
- System.out.println(localDateTime);
- System.out.println(localDateTime2);
- System.out.println(DateTimeCalculatorUtil.betweenTotalDays(localDateTime, localDateTime2));
- System.out.println(DateTimeCalculatorUtil.betweenTotalHours(localDateTime, localDateTime2));
- System.out.println(DateTimeCalculatorUtil.betweenTotalMinutes(localDateTime, localDateTime2));
- System.out.println(DateTimeCalculatorUtil.betweenTotalSeconds(localDateTime, localDateTime2));
- System.out.println(DateTimeCalculatorUtil.betweenTotalMillis(localDateTime, localDateTime2));
- System.out.println(DateTimeCalculatorUtil.betweenTotalNanos(localDateTime, localDateTime2));
- }
- /**
- * 使用Duration比较2个Date
- */
- @Test
- public void dateCalculatorDurationBetweenTest2(){
- Date date = new Date();
- LocalDate localDate2 = LocalDate.of(2021, 3, 7);
- Date date2 = DateTimeConverterUtil.toDate(localDate2);
- System.out.println(date);
- System.out.println(date2);
- System.out.println(DateTimeCalculatorUtil.betweenTotalDays(date, date2));
- System.out.println(DateTimeCalculatorUtil.betweenTotalHours(date, date2));
- System.out.println(DateTimeCalculatorUtil.betweenTotalMinutes(date, date2));
- System.out.println(DateTimeCalculatorUtil.betweenTotalSeconds(date, date2));
- System.out.println(DateTimeCalculatorUtil.betweenTotalMillis(date, date2));
- System.out.println(DateTimeCalculatorUtil.betweenTotalNanos(date, date2));
- }
测试结果:
- 2020-02-06
- 2021-03-07
- 1
- 1
- 1
- Thu Feb 06 22:09:38 CST 2020
- Sun Mar 07 00:00:00 CST 2021
- 1
- 1
- 1
- 2020-02-06T22:09:48.247
- 2021-03-07T22:10:10
- 395
- 9480
- 568800
- 34128021
- 34128021753
- 34128021753000000
- Thu Feb 06 22:09:58 CST 2020
- Sun Mar 07 00:00:00 CST 2021
- 394
- 9457
- 567470
- 34048201
- 34048201995
- 34048201995000000
源代码地址:https://github.com/xkzhangsan/xk-time
Java日期时间API系列17-----Jdk8中java.time包中的新的日期时间API类,java日期计算4,2个日期对比,获取相差年月日部分属性和相差总的天时分秒毫秒纳秒等的更多相关文章
- API接口自动化之3 同一个war包中多个接口做自动化测试
同一个war包中多个接口做自动化测试 一个接口用一个测试类,每个测试用例如下,比如下面是4个测试用例,每个详细的测试用例中含有请求入参,返回体校验,以此来判断每条测试用例是否通过 一个war包中,若含 ...
- Andriod项目开发实战(1)——如何在Eclipse中的一个包下建新包
最开始是想将各个类分门别类地存放在不同的包中,所以想在项目源码包中新建几个不同功能的包eg:utils.model.receiver等,最后的结果应该是下图左边这样的: 很明显建立项目后的架构是上 ...
- Mac 如何导出ipa文件中Assets.car包中的切图
在之前 获取 AppStore 中 应用 的 IPA 包文件(Mac OS 13+)中获取到应用的 IPA 包,可以取出应用的部分图片(如 Logo),如果项目工程中把图片添加到 Assets.xca ...
- C# 计时器 以“天时分秒毫秒”形式动态增加显示
参考:http://zhidao.baidu.com/link?url=j-jxQJenrO54BSKJ_IkXWbhdDqbVLUyyenjjSGs8G0xdisgBZ0EMhzyWgARSFct6 ...
- JDK中的Atomic包中的类及使用
引言 Java从JDK1.5开始提供了java.util.concurrent.atomic包,方便程序员在多线程环境下,无锁的进行原子操作.原子变量的底层使用了处理器提供的原子指令,但是不同的CPU ...
- 【转】Eclipse中查看jar包中的源码
(简单的方式:通过jd-gui来进行反编译,最简单!,参考我的另一篇博文, 地址:http://www.cnblogs.com/gmq-sh/p/4277991.html) Java Decompil ...
- Package.json中dependencies依赖包中^符号和~符号前缀的区别
刚git了webpack的包发现package.json里面dependencies依赖包的版本号前面的符号有两种,一种是~,一种是^,如下图标记: 然后搜了下在stackoverflow上找到一个比 ...
- Java中常见的包
目录 JDK自带的包 第三方包 JDK自带的包 JAVA提供了强大的应用程序接口,既JAVA类库.他包含大量已经设计好的工具类,帮助程序员进行字符串处理.绘图.数学计算和网络应用等方面的工作.下面简单 ...
- Redis总结(五)缓存雪崩和缓存穿透等问题 Web API系列(三)统一异常处理 C#总结(一)AutoResetEvent的使用介绍(用AutoResetEvent实现同步) C#总结(二)事件Event 介绍总结 C#总结(三)DataGridView增加全选列 Web API系列(二)接口安全和参数校验 RabbitMQ学习系列(六): RabbitMQ 高可用集群
Redis总结(五)缓存雪崩和缓存穿透等问题 前面讲过一些redis 缓存的使用和数据持久化.感兴趣的朋友可以看看之前的文章,http://www.cnblogs.com/zhangweizhon ...
- Web API系列(三)统一异常处理
前面讲了webapi的安全验证和参数安全,不清楚的朋友,可以看看前面的文章,<Web API系列(二)接口安全和参数校验>,本文主要介绍Web API异常结果的处理.作为内部或者是对外提供 ...
随机推荐
- 第九讲: MySQL为什么有时候会选错索引?
第九讲: MySQL为什么有时候会选错索引? 前面我们介绍过索引,你已经知道了在 MySQL 中一张表其实是可以支持多个索引的. 但是,你写 SQL 语句的时候,并没有主动指定使用哪个索引.也 ...
- 如何将AI模型与CAE(计算机辅助工程)结合 —— AI大模型能否用于CAE有限元分析和数值模拟仿真的工业软件领域?
引自: https://www.zhihu.com/question/611863569/answer/3271029434?utm_id=0 有限元分析中的三个要素,几何模型,本构模型和边界条件. ...
- AI领域的国产显卡如何在现有技术下吸引用户 —— 廉价增加显存 —— 大显存
先给出一个不大准确的但相差不差的背景介绍: 同样性能级别的显卡,NVIDA的24G的要3W,32G的要5W,48G的要7W, 80G的要10W. 国产同同性能的显卡32G的要10W,48G的要15W, ...
- 如何在Ubuntu系统中进行系统级的代理设置
在Ubuntu系统中我们往往需要设置代理上网,比如在Ubuntu22.04中,通过图形化界面的设置方式如下: 这里假设代理服务器地址为: 192.168.1.103:1080 ============ ...
- git在idea中的冲突解决(非常重要)
1.什么是冲突 冲突是指当你在提交或者更新代码时被合并的文件与当前文件不一致.读起来有点绕,结合下面的案例理解. 从上面对冲突的定义来看,冲突时发生在同一个文件上的. 2.生产上冲突的场景 常见冲突的 ...
- PHPExcel 使用学习
基本实现步骤: <?php require "/PHPExcel/PHPExcel.php";//引入PHPExcel $objPHPExcel = new PHPExcel ...
- Catlan--卡特兰数--组合数学
卡特兰数 \(Catlan\) ·赘述 其实发现卡特兰数和之前不同的是,前面的是给你公式,让你去求具体的例子,然而卡特兰数这里是给你大量例子来给你证明和解释什么是卡特兰数. ·定义 对于卡特兰数来说, ...
- Diskpart 操作
DiskPart 是 Windows 操作系统中的一个命令行工具,用于管理磁盘分区.它可以创建.删除.格式化和调整分区大小,还可以设置活动分区等.以下是一些常用的 DiskPart 命令和操作步骤. ...
- 如何阅读 diff 命令的输出
diff 命令有三种模式:上下文模式(context),合并模式(unified)和普通模式(normal).其中最常用的是合并模式. 合并模式 diff -u f1 f2 --- f1 2024-0 ...
- 【Mac + Appium + Java1.8(三)】之IOS自动化环境安装配置以及简单测试用例编写(模拟器、真机)
前提条件: =========================================== 1.Xcode版本为Xcode10及以上2.Appium版本必须为1.9及以上,因为Xcode为10 ...