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异常结果的处理.作为内部或者是对外提供 ...
随机推荐
- Jmeter函数助手33-split
split函数用于根据分隔符拆分传递给它的字符串,并返回原始字符串. String to split:填入需要转换的字符串 函数名称.用于存储在测试计划中其他的方式使用的值:存储结果的变量名 Stri ...
- .NET 结果与错误处理利器 FluentResults
前言 在项目开发中,方法返回的结果(成功或失败)对我们开发来说很重要.传统方法,如通过异常来指示错误或使用特定的返回类型(如布尔值加输出参数),虽然有效,但可能缺乏直观性和灵活性. FluentRes ...
- 【Spring】02 过程分析
回顾JavaWeb三层架构设计: UserDao接口 public interface UserDao { void getUser(); } 实现类 public class UserDaoImpl ...
- 【C】Re10 共用体、枚举、类型定义
Union 联合体\共用体 结合体: 每个成员的内存空间都是独立存在的,互不干涉 int 成员.double 成员.char 成员 = 4 + 8 + 1 [13字节大小] 取成员字节占用之和作为这个 ...
- 【转载】 Tensorflow如何直接使用预训练模型(vgg16为例)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/weixin_44633882/artic ...
- 树莓派3b+ 安装Ubuntu mate18.04系统 配置SSH 、 远程桌面realvnc
Ubuntu mate for 树莓派3b+ 系统下载: 下载地址: https://ubuntu-mate.org/download/ 这里用32位系统:(64位也可以的,虽然还是实验阶段的,不 ...
- AI实践者师生夏令营讲座视频:南京大学Lamda实验室(周志华 团队)讲座视频 —— 强化学习的局限性与展望
视频地址: 周志华团队与Intel团队的讲座视频--强化学习的局限性与未来展望 视频链接地址: https://bizwebcast.smarket.com.cn/b975d6d9969a42cba9 ...
- Ubuntu18.04动态桌面壁纸variety的安装
Ubuntu18.04桌面系统,安装动态更换桌面壁纸的应用,即安装软件 variety. variety 的安装: sudo apt install variety 安装好以后就会自动启动. 我们可以 ...
- 实验室服务彻底死机记录——硬件故障——主板pcie槽坏掉或显卡坏掉
2022年11月8日 后记(最新更新) 服务器送售后,售后给厂家技术打电话,厂家技术说可能是显卡的电源线松了,于是我们打开机箱把显卡的电源线紧了紧,然后神奇的事情发生了,故障解除了...... 一 ...
- 读论文《IMPALA: Scalable Distributed Deep-RL with Importance Weighted Actor-Learner Architectures》
论文地址: https://arxiv.org/pdf/1802.01561v2.pdf 论文<IMPALA: Scalable Distributed Deep-RL with Importa ...