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异常结果的处理.作为内部或者是对外提供 ...
随机推荐
- 如何在Arch Linux上构建Raspberry Pi虚拟环境
如何在Linux上构建Raspberry Pi虚拟环境 下面我们来讲讲如何使用QEMU来仿照树莓派环境.这里首先先分成两大类.第一类是跑比较老的,安全性较低的老树莓派,主要指代的是22年4月份发布 ...
- Python编写html文件
背景:部门需要发送周报.月报,每次都需要去数据库导出数据整理统计发送给领导,人工操作显得繁琐且费时间. 1.可以定时用python将数据库查询数据结果写成html文件,达到浏览器访问的效果,定时发送给 ...
- 轻松搞定 Nginx 在 CentOS 和 Ubuntu 上的安装与配置
注:这是对我以前博客进行优化后再次发布的,博客中的截图为以前的.原博客已删除. 如何安装nginx nginx是一款开源.高性能的Web和反向代理服务器,支持HTTP.HTTPS.SMTP.POP3和 ...
- 【FastDFS】环境搭建 01 跟踪器和存储节点
FastDFS:分布式文件系统 它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题. 特别适合以文件为载体的在线服务,如相册网站.视频网 ...
- 强化学习、分布式计算方向的phd毕业后去企业的要求
实验室慕师弟马上要phd毕业了,虽然我是遥遥无期,但是看到身边同学可以上岸还是提师弟高兴.由于师弟准备去企业工作,于是乎我也不免好奇起来phd毕业后去公司会有什么样的要求,于是网上找了找招聘信息,挑了 ...
- 神经网络初始化:xavier,kaiming、ortho正交初始化在CNN网络中的使用
xavier.ortho是神经网络中常用的权重初始化方法,在全连接中这两种权重初始化的方法比较好理解,但是在CNN的卷积网络中的具体实现却不好理解了. 在CNN网络中xavier的初始化可以参看: [ ...
- springboot整合validation统一参数检查
1.背景 实际开发中对参数进行检查,是常见 比如如下代码 /** * 参数检查测试(传统做法) * * @param dto * @return */ @GetMapping("/param ...
- FFmpeg开发笔记(四十五)使用SRT Streamer开启APP直播推流
SRT Streamer是一个安卓手机端的开源SRT协议直播推流框架,可用于RTMP直播和SRT直播.SRT Streamer支持的视频编码包括H264.H265等等,支持的音频编码包括AAC.OP ...
- SMCA:港中文提出注意力图校准的DETR加速方案 | ICCV 2021
为了加速DETR收敛,论文提出了简单而有效的Spatially Modulated Co-Attention(SMCA)机制,通过在初始边界框位置给予较高的协同注意力响应值的约束来构建DETR的回归感 ...
- C语言实现url解析小实例
一.前言 前面一口君写了一篇关于url的文章: <一文带你理解URI 和 URL 有什么区别?> 本篇在此基础上,编写一个简单的用于解析url的小例子, 最终目标是解析出URL中所有的数据 ...