通过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. k8s 环境搭建(2)

    安装docker组件 配置本地源或者自带的网络源2选1 1.切换镜像源 wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce ...

  2. Jupyter Lab和Jupyter Notebook的区别

    JupyterLab与Jupyter Notebook:详细比较 简介 Jupyter Notebook是一个开源的Web应用程序,允许用户创建和共享包含实时代码.方程.可视化和解释性文本的文档.Ju ...

  3. 【转载】 PID算法的解析

    原文来自DF创客社区地址:http://www.dfrobot.com.cn/community/thread-14783-1-1.html ----------------------------- ...

  4. 【转载】 【Java分享客栈】我曾经的两个Java老师一个找不到工作了一个被迫转行了

      本文作者: 福隆苑居士 本文链接: https://www.cnblogs.com/fulongyuanjushi/p/16182465.html 版权声明: 本博客所有文章除特别声明外,均采用 ...

  5. 【转载】python的魔法方法———A Guide to Python's Magic Methods

    原文地址: https://rszalski.github.io/magicmethods/ ===================================================== ...

  6. Keil uVision5软件破解方法

    1.正常下载软件 2.右键"管理员方式运行"软件 3.如下图"File"->"License..." 4.复制CID,管理员方式打开破 ...

  7. 2024 (ICPC) Jiangxi Provincial 省赛

    2024 (ICPC) Jiangxi Provincial 省赛 前言 和队友 vp 7t,赛后补了几题. A. Maliang Learning Painting 思路 输出 a + b + c ...

  8. CORDIC算法解释及FPGA实现(圆坐标系)

    CORDIC算法解释及Verilog仿真(圆坐标系) CORDIC算法原理阐述 CORDIC(Coordinate Rotation Digital Computer)算法,即坐标旋转数字计算方法,是 ...

  9. C#反射在数据库连接时的应用

    反射的应用 1.查找指定ID的数据 //查找指定id的数据,返回指定的对象 private T FindData<T>(int id) { Type ty = typeof(T); //在 ...

  10. 实用接地气的 .NET 微服务框架

    前言 微服务架构已经成为搭建高效.可扩展系统的关键技术之一,然而,现有许多微服务框架往往过于复杂,使得我们普通开发者难以快速上手并体验到微服务带了的便利.为了解决这一问题,于是作者精心打造了一款最接地 ...