通过Java日期时间API系列9-----Jdk8中java.time包中的新的日期时间API类的Period和Duration的区别 ,可以看出java8设计非常好,新增了Period和Duration类,专用于对比2个时间场景:

Period,可以获取2个时间相差的年月日的属性。

Duration,可以获取2个时间相差总的天时分秒毫秒纳秒。

下面应用:

  1. /**
  2. * 获取2个日期的相差年月天的年数部分
  3. * @param startInclusive
  4. * @param endExclusive
  5. * @return
  6. */
  7. public static long betweenYears(LocalDateTime startInclusive, LocalDateTime endExclusive){
  8. Objects.requireNonNull(startInclusive, "startInclusive");
  9. Objects.requireNonNull(endExclusive, "endExclusive");
  10. return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
  11. DateTimeConverterUtil.toLocalDate(endExclusive)).getYears();
  12. }
  13.  
  14. /**
  15. * 获取2个日期的相差年月天的年数部分
  16. * @param startInclusive
  17. * @param endExclusive
  18. * @return
  19. */
  20. public static long betweenYears(Date startInclusive, Date endExclusive){
  21. Objects.requireNonNull(startInclusive, "startInclusive");
  22. Objects.requireNonNull(endExclusive, "endExclusive");
  23. return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
  24. DateTimeConverterUtil.toLocalDate(endExclusive)).getYears();
  25. }
  26.  
  27. /**
  28. * 获取2个日期的相差年月天的年数部分
  29. * @param startInclusive
  30. * @param endExclusive
  31. * @return
  32. */
  33. public static long betweenYears(LocalDate startInclusive, LocalDate endExclusive){
  34. return Period.between(startInclusive, endExclusive).getYears();
  35. }
  36.  
  37. /**
  38. * 获取2个日期的相差年月天的月数部分
  39. * @param startInclusive
  40. * @param endExclusive
  41. * @return
  42. */
  43. public static long betweenMonths(LocalDateTime startInclusive, LocalDateTime endExclusive){
  44. Objects.requireNonNull(startInclusive, "startInclusive");
  45. Objects.requireNonNull(endExclusive, "endExclusive");
  46. return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
  47. DateTimeConverterUtil.toLocalDate(endExclusive)).getMonths();
  48. }
  49.  
  50. /**
  51. * 获取2个日期的相差年月天的月数部分
  52. * @param startInclusive
  53. * @param endExclusive
  54. * @return
  55. */
  56. public static long betweenMonths(Date startInclusive, Date endExclusive){
  57. Objects.requireNonNull(startInclusive, "startInclusive");
  58. Objects.requireNonNull(endExclusive, "endExclusive");
  59. return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
  60. DateTimeConverterUtil.toLocalDate(endExclusive)).getMonths();
  61. }
  62.  
  63. /**
  64. * 获取2个日期的相差年月天的月数部分
  65. * @param startInclusive
  66. * @param endExclusive
  67. * @return
  68. */
  69. public static long betweenMonths(LocalDate startInclusive, LocalDate endExclusive){
  70. return Period.between(startInclusive, endExclusive).getMonths();
  71. }
  72.  
  73. /**
  74. * 获取2个日期的相差年月天的天数部分
  75. * @param startInclusive
  76. * @param endExclusive
  77. * @return
  78. */
  79. public static long betweenDays(LocalDateTime startInclusive, LocalDateTime endExclusive){
  80. Objects.requireNonNull(startInclusive, "startInclusive");
  81. Objects.requireNonNull(endExclusive, "endExclusive");
  82. return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
  83. DateTimeConverterUtil.toLocalDate(endExclusive)).getDays();
  84. }
  85.  
  86. /**
  87. * 获取2个日期的相差年月天的天数部分
  88. * @param startInclusive
  89. * @param endExclusive
  90. * @return
  91. */
  92. public static long betweenDays(Date startInclusive, Date endExclusive){
  93. Objects.requireNonNull(startInclusive, "startInclusive");
  94. Objects.requireNonNull(endExclusive, "endExclusive");
  95. return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
  96. DateTimeConverterUtil.toLocalDate(endExclusive)).getDays();
  97. }
  98.  
  99. /**
  100. * 获取2个日期的相差年月天的天数部分
  101. * @param startInclusive
  102. * @param endExclusive
  103. * @return
  104. */
  105. public static long betweenDays(LocalDate startInclusive, LocalDate endExclusive){
  106. return Period.between(startInclusive, endExclusive).getDays();
  107. }
  108.  
  109. /**
  110. * 获取2个日期的相差总天数
  111. * @param startInclusive
  112. * @param endExclusive
  113. * @return
  114. */
  115. public static long betweenTotalDays(LocalDateTime startInclusive, LocalDateTime endExclusive){
  116. return Duration.between(startInclusive, endExclusive).toDays();
  117. }
  118.  
  119. /**
  120. * 获取2个日期的相差总天数
  121. * @param startInclusive
  122. * @param endExclusive
  123. * @return
  124. */
  125. public static long betweenTotalDays(Date startInclusive, Date endExclusive){
  126. Objects.requireNonNull(startInclusive, "startInclusive");
  127. Objects.requireNonNull(endExclusive, "endExclusive");
  128. return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toDays();
  129. }
  130.  
  131. /**
  132. * 获取2个日期的相差总小时数
  133. * @param startInclusive
  134. * @param endExclusive
  135. * @return
  136. */
  137. public static long betweenTotalHours(LocalDateTime startInclusive, LocalDateTime endExclusive){
  138. return Duration.between(startInclusive, endExclusive).toHours();
  139. }
  140.  
  141. /**
  142. * 获取2个日期的相差总小时数
  143. * @param startInclusive
  144. * @param endExclusive
  145. * @return
  146. */
  147. public static long betweenTotalHours(LocalTime startInclusive, LocalTime endExclusive){
  148. return Duration.between(startInclusive, endExclusive).toHours();
  149. }
  150.  
  151. /**
  152. * 获取2个日期的相差总小时数
  153. * @param startInclusive
  154. * @param endExclusive
  155. * @return
  156. */
  157. public static long betweenTotalHours(Date startInclusive, Date endExclusive){
  158. Objects.requireNonNull(startInclusive, "startInclusive");
  159. Objects.requireNonNull(endExclusive, "endExclusive");
  160. return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toHours();
  161. }
  162.  
  163. /**
  164. * 获取2个日期的相差总分钟数
  165. * @param startInclusive
  166. * @param endExclusive
  167. * @return
  168. */
  169. public static long betweenTotalMinutes(LocalDateTime startInclusive, LocalDateTime endExclusive){
  170. return Duration.between(startInclusive, endExclusive).toMinutes();
  171. }
  172.  
  173. /**
  174. * 获取2个日期的相差总分钟数
  175. * @param startInclusive
  176. * @param endExclusive
  177. * @return
  178. */
  179. public static long betweenTotalMinutes(LocalTime startInclusive, LocalTime endExclusive){
  180. return Duration.between(startInclusive, endExclusive).toMinutes();
  181. }
  182.  
  183. /**
  184. * 获取2个日期的相差总分钟数
  185. * @param startInclusive
  186. * @param endExclusive
  187. * @return
  188. */
  189. public static long betweenTotalMinutes(Date startInclusive, Date endExclusive){
  190. Objects.requireNonNull(startInclusive, "startInclusive");
  191. Objects.requireNonNull(endExclusive, "endExclusive");
  192. return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toMinutes();
  193. }
  194.  
  195. /**
  196. * 获取2个日期的相差总秒数
  197. * @param startInclusive
  198. * @param endExclusive
  199. * @return
  200. */
  201. public static long betweenTotalSeconds(LocalDateTime startInclusive, LocalDateTime endExclusive){
  202. return Duration.between(startInclusive, endExclusive).getSeconds();
  203. }
  204.  
  205. /**
  206. * 获取2个日期的相差总秒数
  207. * @param startInclusive
  208. * @param endExclusive
  209. * @return
  210. */
  211. public static long betweenTotalSeconds(LocalTime startInclusive, LocalTime endExclusive){
  212. return Duration.between(startInclusive, endExclusive).getSeconds();
  213. }
  214.  
  215. /**
  216. * 获取2个日期的相差总秒数
  217. * @param startInclusive
  218. * @param endExclusive
  219. * @return
  220. */
  221. public static long betweenTotalSeconds(Date startInclusive, Date endExclusive){
  222. Objects.requireNonNull(startInclusive, "startInclusive");
  223. Objects.requireNonNull(endExclusive, "endExclusive");
  224. return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).getSeconds();
  225. }
  226.  
  227. /**
  228. * 获取2个日期的相差总毫秒数
  229. * @param startInclusive
  230. * @param endExclusive
  231. * @return
  232. */
  233. public static long betweenTotalMillis(LocalDateTime startInclusive, LocalDateTime endExclusive){
  234. return Duration.between(startInclusive, endExclusive).toMillis();
  235. }
  236.  
  237. /**
  238. * 获取2个日期的相差总毫秒数
  239. * @param startInclusive
  240. * @param endExclusive
  241. * @return
  242. */
  243. public static long betweenTotalMillis(LocalTime startInclusive, LocalTime endExclusive){
  244. return Duration.between(startInclusive, endExclusive).toMillis();
  245. }
  246.  
  247. /**
  248. * 获取2个日期的相差总毫秒数
  249. * @param startInclusive
  250. * @param endExclusive
  251. * @return
  252. */
  253. public static long betweenTotalMillis(Date startInclusive, Date endExclusive){
  254. Objects.requireNonNull(startInclusive, "startInclusive");
  255. Objects.requireNonNull(endExclusive, "endExclusive");
  256. return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toMillis();
  257. }
  258.  
  259. /**
  260. * 获取2个日期的相差总纳秒数
  261. * @param startInclusive
  262. * @param endExclusive
  263. * @return
  264. */
  265. public static long betweenTotalNanos(LocalDateTime startInclusive, LocalDateTime endExclusive){
  266. return Duration.between(startInclusive, endExclusive).toNanos();
  267. }
  268.  
  269. /**
  270. * 获取2个日期的相差总纳秒数
  271. * @param startInclusive
  272. * @param endExclusive
  273. * @return
  274. */
  275. public static long betweenTotalNanos(LocalTime startInclusive, LocalTime endExclusive){
  276. return Duration.between(startInclusive, endExclusive).toNanos();
  277. }
  278.  
  279. /**
  280. * 获取2个日期的相差总纳秒数
  281. * @param startInclusive
  282. * @param endExclusive
  283. * @return
  284. */
  285. public static long betweenTotalNanos(Date startInclusive, Date endExclusive){
  286. Objects.requireNonNull(startInclusive, "startInclusive");
  287. Objects.requireNonNull(endExclusive, "endExclusive");
  288. return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toNanos();
  289. }

测试代码

  1. /**
  2. * 使用Period比较2个LocalDate
  3. */
  4. @Test
  5. public void dateCalculatorPeriodBetweenTest(){
  6. LocalDate localDate = LocalDate.now();
  7. LocalDate localDate2 = LocalDate.of(2021, 3, 7);
  8. System.out.println(localDate);
  9. System.out.println(localDate2);
  10.  
  11. System.out.println(DateTimeCalculatorUtil.betweenYears(localDate, localDate2));
  12. System.out.println(DateTimeCalculatorUtil.betweenMonths(localDate, localDate2));
  13. System.out.println(DateTimeCalculatorUtil.betweenDays(localDate, localDate2));
  14. }
  15.  
  16. /**
  17. * 使用Period比较2个Date
  18. */
  19. @Test
  20. public void dateCalculatorPeriodBetweenTest2(){
  21. Date date = new Date();
  22. LocalDate localDate2 = LocalDate.of(2021, 3, 7);
  23. Date date2 = DateTimeConverterUtil.toDate(localDate2);
  24. System.out.println(date);
  25. System.out.println(date2);
  26.  
  27. System.out.println(DateTimeCalculatorUtil.betweenYears(date, date2));
  28. System.out.println(DateTimeCalculatorUtil.betweenMonths(date, date2));
  29. System.out.println(DateTimeCalculatorUtil.betweenDays(date, date2));
  30. }
  31.  
  32. /**
  33. * 使用Duration比较2个LocalDateTime
  34. */
  35. @Test
  36. public void dateCalculatorDurationBetweenTest(){
  37. LocalDateTime localDateTime = LocalDateTime.now();
  38. LocalDateTime localDateTime2 = LocalDateTime.of(2021, 3, 7, 22, 10, 10);
  39. System.out.println(localDateTime);
  40. System.out.println(localDateTime2);
  41.  
  42. System.out.println(DateTimeCalculatorUtil.betweenTotalDays(localDateTime, localDateTime2));
  43. System.out.println(DateTimeCalculatorUtil.betweenTotalHours(localDateTime, localDateTime2));
  44. System.out.println(DateTimeCalculatorUtil.betweenTotalMinutes(localDateTime, localDateTime2));
  45. System.out.println(DateTimeCalculatorUtil.betweenTotalSeconds(localDateTime, localDateTime2));
  46. System.out.println(DateTimeCalculatorUtil.betweenTotalMillis(localDateTime, localDateTime2));
  47. System.out.println(DateTimeCalculatorUtil.betweenTotalNanos(localDateTime, localDateTime2));
  48. }
  49.  
  50. /**
  51. * 使用Duration比较2个Date
  52. */
  53. @Test
  54. public void dateCalculatorDurationBetweenTest2(){
  55. Date date = new Date();
  56. LocalDate localDate2 = LocalDate.of(2021, 3, 7);
  57. Date date2 = DateTimeConverterUtil.toDate(localDate2);
  58. System.out.println(date);
  59. System.out.println(date2);
  60.  
  61. System.out.println(DateTimeCalculatorUtil.betweenTotalDays(date, date2));
  62. System.out.println(DateTimeCalculatorUtil.betweenTotalHours(date, date2));
  63. System.out.println(DateTimeCalculatorUtil.betweenTotalMinutes(date, date2));
  64. System.out.println(DateTimeCalculatorUtil.betweenTotalSeconds(date, date2));
  65. System.out.println(DateTimeCalculatorUtil.betweenTotalMillis(date, date2));
  66. System.out.println(DateTimeCalculatorUtil.betweenTotalNanos(date, date2));
  67. }

测试结果:

  1. 2020-02-06
  2. 2021-03-07
  3. 1
  4. 1
  5. 1
  6.  
  7. Thu Feb 06 22:09:38 CST 2020
  8. Sun Mar 07 00:00:00 CST 2021
  9. 1
  10. 1
  11. 1
  12.  
  13. 2020-02-06T22:09:48.247
  14. 2021-03-07T22:10:10
  15. 395
  16. 9480
  17. 568800
  18. 34128021
  19. 34128021753
  20. 34128021753000000
  21.  
  22. Thu Feb 06 22:09:58 CST 2020
  23. Sun Mar 07 00:00:00 CST 2021
  24. 394
  25. 9457
  26. 567470
  27. 34048201
  28. 34048201995
  29. 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. 第九讲: MySQL为什么有时候会选错索引?

    第九讲: MySQL为什么有时候会选错索引? ​ 前面我们介绍过索引,你已经知道了在 MySQL 中一张表其实是可以支持多个索引的. ​ 但是,你写 SQL 语句的时候,并没有主动指定使用哪个索引.也 ...

  2. 如何将AI模型与CAE(计算机辅助工程)结合 —— AI大模型能否用于CAE有限元分析和数值模拟仿真的工业软件领域?

    引自: https://www.zhihu.com/question/611863569/answer/3271029434?utm_id=0 有限元分析中的三个要素,几何模型,本构模型和边界条件. ...

  3. AI领域的国产显卡如何在现有技术下吸引用户 —— 廉价增加显存 —— 大显存

    先给出一个不大准确的但相差不差的背景介绍: 同样性能级别的显卡,NVIDA的24G的要3W,32G的要5W,48G的要7W, 80G的要10W. 国产同同性能的显卡32G的要10W,48G的要15W, ...

  4. 如何在Ubuntu系统中进行系统级的代理设置

    在Ubuntu系统中我们往往需要设置代理上网,比如在Ubuntu22.04中,通过图形化界面的设置方式如下: 这里假设代理服务器地址为: 192.168.1.103:1080 ============ ...

  5. git在idea中的冲突解决(非常重要)

    1.什么是冲突 冲突是指当你在提交或者更新代码时被合并的文件与当前文件不一致.读起来有点绕,结合下面的案例理解. 从上面对冲突的定义来看,冲突时发生在同一个文件上的. 2.生产上冲突的场景 常见冲突的 ...

  6. PHPExcel 使用学习

    基本实现步骤: <?php require "/PHPExcel/PHPExcel.php";//引入PHPExcel $objPHPExcel = new PHPExcel ...

  7. Catlan--卡特兰数--组合数学

    卡特兰数 \(Catlan\) ·赘述 其实发现卡特兰数和之前不同的是,前面的是给你公式,让你去求具体的例子,然而卡特兰数这里是给你大量例子来给你证明和解释什么是卡特兰数. ·定义 对于卡特兰数来说, ...

  8. Diskpart 操作

    DiskPart 是 Windows 操作系统中的一个命令行工具,用于管理磁盘分区.它可以创建.删除.格式化和调整分区大小,还可以设置活动分区等.以下是一些常用的 DiskPart 命令和操作步骤. ...

  9. 如何阅读 diff 命令的输出

    diff 命令有三种模式:上下文模式(context),合并模式(unified)和普通模式(normal).其中最常用的是合并模式. 合并模式 diff -u f1 f2 --- f1 2024-0 ...

  10. 【Mac + Appium + Java1.8(三)】之IOS自动化环境安装配置以及简单测试用例编写(模拟器、真机)

    前提条件: =========================================== 1.Xcode版本为Xcode10及以上2.Appium版本必须为1.9及以上,因为Xcode为10 ...