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异常结果的处理.作为内部或者是对外提供 ...
随机推荐
- 8、IDEA集成Git
8.1.配置Git忽略文件 8.1.1.忽略文件的原因 在使用 IDE 工具时,会自动生成一些和项目源码无关的文件,所以可以让 Git 忽略这些文件. 此外,把这些无关文件忽略掉,还能够屏蔽不同 ID ...
- 【Java】实体类转换框架 MapStruct
简单尝试了下发现比Dozer还有BeanUtil还方便小巧 注解的作用是在生成字节码文件时实现具体GetterSetter方法,实际转换时就是赋值操作,嘎嘎快 参考文章: https://juejin ...
- 如何使用工具下载B站非会员视频(下载B站免费web视频)
最近准备从B站上下载几个web页面上的视频,但是B站的视频又没有提供相关的下载工具,于是找到了一款下载B站视频的工具( you-get ), 该工具不能下载会员版的视频,不能下载收费的视频,不过对于免 ...
- python进程绑定CPU的意义
1. 绑定CPU后对计算密集型的任务可能会一定程度上提升运算性能:(小幅度的性能提升,甚至小幅度落后,总之就是差别不大) 对比1代码A: import os from multiprocessing ...
- vue之组件的简单使用
1.背景 2.组件的简单使用 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- 海豚²来了丨DolphinDB 集成 DolphinScheduler,任务调度更轻松
DolphinDB 是一款高性能时序数据库.DolphinDB 集成了功能强大的编程语言和高容量高速度的批流一体数据分析系统,为海量数据(特别是时间序列数据)的快速存储.检索.计算及分析提供一站式解决 ...
- Lucas-Washburn + Cassie-Baxter
如果粉末间隙内壁的表面能随着润湿而降低,则液体会向管内上升渗入(\(\gamma_{\text{SL}}<\gamma_{\text{SO}}\)). 考虑液体上升的驱动力来自于附加压力,则由弯 ...
- Diskpart 操作
DiskPart 是 Windows 操作系统中的一个命令行工具,用于管理磁盘分区.它可以创建.删除.格式化和调整分区大小,还可以设置活动分区等.以下是一些常用的 DiskPart 命令和操作步骤. ...
- docker context 介绍
$ docker context ls NAME DESCRIPTION DOCKER ENDPOINT ERROR default Current DOCKER_HOST based configu ...
- [golang]查询ssl证书剩余有效天数并邮件提醒
前言 自从云厂商的免费ssl证书改成3个月,而且证书数量还是20个之后,自己网站的ssl证书就换成了其它免费方案.但是免费方案不会提醒证书过期,所以写个工具每天定时查询证书剩余有效天数,如果证书即将过 ...