通过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个日期对比,获取相差年月日部分属性和相差总的天时分秒毫秒纳秒等的更多相关文章

  1. API接口自动化之3 同一个war包中多个接口做自动化测试

    同一个war包中多个接口做自动化测试 一个接口用一个测试类,每个测试用例如下,比如下面是4个测试用例,每个详细的测试用例中含有请求入参,返回体校验,以此来判断每条测试用例是否通过 一个war包中,若含 ...

  2. Andriod项目开发实战(1)——如何在Eclipse中的一个包下建新包

    最开始是想将各个类分门别类地存放在不同的包中,所以想在项目源码包中新建几个不同功能的包eg:utils.model.receiver等,最后的结果应该是下图左边这样的:   很明显建立项目后的架构是上 ...

  3. Mac 如何导出ipa文件中Assets.car包中的切图

    在之前 获取 AppStore 中 应用 的 IPA 包文件(Mac OS 13+)中获取到应用的 IPA 包,可以取出应用的部分图片(如 Logo),如果项目工程中把图片添加到 Assets.xca ...

  4. C# 计时器 以“天时分秒毫秒”形式动态增加显示

    参考:http://zhidao.baidu.com/link?url=j-jxQJenrO54BSKJ_IkXWbhdDqbVLUyyenjjSGs8G0xdisgBZ0EMhzyWgARSFct6 ...

  5. JDK中的Atomic包中的类及使用

    引言 Java从JDK1.5开始提供了java.util.concurrent.atomic包,方便程序员在多线程环境下,无锁的进行原子操作.原子变量的底层使用了处理器提供的原子指令,但是不同的CPU ...

  6. 【转】Eclipse中查看jar包中的源码

    (简单的方式:通过jd-gui来进行反编译,最简单!,参考我的另一篇博文, 地址:http://www.cnblogs.com/gmq-sh/p/4277991.html) Java Decompil ...

  7. Package.json中dependencies依赖包中^符号和~符号前缀的区别

    刚git了webpack的包发现package.json里面dependencies依赖包的版本号前面的符号有两种,一种是~,一种是^,如下图标记: 然后搜了下在stackoverflow上找到一个比 ...

  8. Java中常见的包

    目录 JDK自带的包 第三方包 JDK自带的包 JAVA提供了强大的应用程序接口,既JAVA类库.他包含大量已经设计好的工具类,帮助程序员进行字符串处理.绘图.数学计算和网络应用等方面的工作.下面简单 ...

  9. Redis总结(五)缓存雪崩和缓存穿透等问题 Web API系列(三)统一异常处理 C#总结(一)AutoResetEvent的使用介绍(用AutoResetEvent实现同步) C#总结(二)事件Event 介绍总结 C#总结(三)DataGridView增加全选列 Web API系列(二)接口安全和参数校验 RabbitMQ学习系列(六): RabbitMQ 高可用集群

    Redis总结(五)缓存雪崩和缓存穿透等问题   前面讲过一些redis 缓存的使用和数据持久化.感兴趣的朋友可以看看之前的文章,http://www.cnblogs.com/zhangweizhon ...

  10. Web API系列(三)统一异常处理

    前面讲了webapi的安全验证和参数安全,不清楚的朋友,可以看看前面的文章,<Web API系列(二)接口安全和参数校验>,本文主要介绍Web API异常结果的处理.作为内部或者是对外提供 ...

随机推荐

  1. python修改类属性

    python修改类属性 1,当类属性为不可变的值时,不可以通过实例对象去修改类属性 class Foo(object): x = 1.5 foo = Foo() print(foo.x) #输出:1. ...

  2. 我的编程经历,从天桥地摊Basic到西藏阿里的.Net AOT。(一,从井到Sharp)

    撇清一层歧义:标题中的阿里不是指阿里巴巴集团,喜马拉雅也不是指那个做音频频道的公司,文中所及内容以及我本人都与他们没有任何关联.依照地理正式名称:阿里指的是西藏西部阿里地区,喜马拉雅指的是青藏高原地球 ...

  3. BI 工具助力企业解锁数字化工厂,开启工业智能新视界

    背景 在 2022 年公布的<"十四五"数字经济发展规划>中,政府不断增加对制造业数字化转型的政策支持力度,积极倡导制造企业采用最新技术,提升自动化.数字化和智能化水平 ...

  4. 【SVN】属性功能配置

    一.配置SVN提交模板 更改SVN提交信息模板 (参考源博客): https://www.cnblogs.com/fairylyl/p/10505833.html 右键属性配置: 新建一项属性,选择[ ...

  5. 【Docker】06 部署挂载本地目录的Nginx

    1.拉取Nginx镜像: docker pull nginx:1.19 2.生成一个测试的Nginx容器: docker run --rm --name nginx-test -p 8080:80 - ...

  6. 路径规划综述博客:A* Optimizations and Improvements

    地址: https://lucho1.github.io/JumpPointSearch/ 原作者还开发了A* 算法的Windows系统上的小程序:(重点:小程序意义不大,这个综述还是不赖的) 项目地 ...

  7. AI4Science 再填新成员:谷歌推出天气模型MetNet-3 已落地相关产品、谷歌天气预报模型GraphCast登刊Science —— AI天气预报大模型

    相关: https://zhidx.com/news/40169.html https://zhidx.com/news/40290.html PS. 要知道,华为公司的最高学术成果就是AI天气预报, ...

  8. 安装华为软件昇腾mindspore-gpu-1.9.0失败记录

    官网安装地址: https://www.mindspore.cn/install PS: 不得不说华为的软件是愈发的不好用了,这个mindspore老版本去年我是使用过的,安装也是比较方便的,搞不清这 ...

  9. GAN总结

    GAN总结 本篇文章主要是根据GitHub上的GAN代码库[PyTorch-GAN]进行GAN的复习和回顾,对于之前GAN的各种结构的一种简要的概括. Code 关于评价GAN模型的标准 Incept ...

  10. Wetab 标签页:内置多种免费实用优雅小组件的浏览器主页和起始页

    Wetab 是什么? Wetab 是一款基于浏览器的新标签页产品,主张辅助用户打造一个兼具效率与美观的主页. Wetab 的核心特色便是内置了多种实用.优雅的小组件. 今天这篇,主要按照分类详细介绍  ...