用途:日期计算
使用场景
包含日期的各种计算方式
项目引用
此博文的依据:hutool-5.6.5版本源码
方法摘要
返回值:
日期差
参考案例:
String dateStr1 = "2017-03-01 22:34:23"; Date date1 = DateUtil.parse(dateStr1); String dateStr2 = "2017-04-01 23:56:14"; Date date2 = DateUtil.parse(dateStr2); // 相差月 long betweenMonth = DateUtil.betweenMonth(date1, date2, false); Assert.assertEquals(1, betweenMonth);// 相差一个月 // 反向 betweenMonth = DateUtil.betweenMonth(date2, date1, false); Assert.assertEquals(1, betweenMonth);// 相差一个月 // 相差天 long betweenDay = DateUtil.between(date1, date2, DateUnit.DAY); Assert.assertEquals(31, betweenDay);// 相差一个月,31天 // 反向 betweenDay = DateUtil.between(date2, date1, DateUnit.DAY); Assert.assertEquals(31, betweenDay);// 相差一个月,31天 // 相差小时 long betweenHour = DateUtil.between(date1, date2, DateUnit.HOUR); Assert.assertEquals(745, betweenHour); // 反向 betweenHour = DateUtil.between(date2, date1, DateUnit.HOUR); Assert.assertEquals(745, betweenHour); // 相差分 long betweenMinute = DateUtil.between(date1, date2, DateUnit.MINUTE); Assert.assertEquals(44721, betweenMinute); // 反向 betweenMinute = DateUtil.between(date2, date1, DateUnit.MINUTE); Assert.assertEquals(44721, betweenMinute); // 相差秒 long betweenSecond = DateUtil.between(date1, date2, DateUnit.SECOND); Assert.assertEquals(2683311, betweenSecond); // 反向 betweenSecond = DateUtil.between(date2, date1, DateUnit.SECOND); Assert.assertEquals(2683311, betweenSecond); // 相差秒 long betweenMS = DateUtil.between(date1, date2, DateUnit.MS); Assert.assertEquals(2683311000L, betweenMS); // 反向 betweenMS = DateUtil.between(date2, date1, DateUnit.MS); Assert.assertEquals(2683311000L, betweenMS); long between = DateUtil.between(DateUtil.parse("2019-05-06 02:15:00"), DateUtil.parse("2019-05-06 02:20:00"), DateUnit.HOUR); Assert.assertEquals(0, between);
返回值:
日期差
参考案例:
long between = DateUtil.between(DateUtil.parse("2021-05-18 22:15:00"), DateUtil.parse("2021-05-18 21:15:00"), DateUnit.HOUR,true); Assert.assertEquals(1, between);
返回值:
日期差
参考案例:
//判断两个日期相差的毫秒数 long between = DateUtil.betweenMs(DateUtil.parse("2021-05-18 22:15:00"), DateUtil.parse("2021-05-18 21:15:00")); Assert.assertEquals(60*60*1000, between);
long betweenYear = DateUtil.betweenYear(DateUtil.parse("2021-05-18 22:15:00"), DateUtil.parse("2020-05-18 21:15:00"),true); Assert.assertEquals(1, betweenYear); betweenYear = DateUtil.betweenYear(DateUtil.parse("2020-05-18 22:15:00"), DateUtil.parse("2021-05-18 22:15:00"),true); Assert.assertEquals(1, betweenYear); betweenYear = DateUtil.betweenYear(DateUtil.parse("2020-05-18 22:15:00"), DateUtil.parse("2021-05-18 21:15:00"),false); Assert.assertEquals(0, betweenYear); betweenYear = DateUtil.betweenYear(DateUtil.parse("2020-05-18 22:15:00"), DateUtil.parse("2021-05-19 22:15:00"),false); Assert.assertEquals(1, betweenYear); betweenYear = DateUtil.betweenYear(DateUtil.parse("2020-05-18 22:15:00"), DateUtil.parse("2021-04-18 22:15:00"),false); Assert.assertEquals(0, betweenYear);
TimeInterval timer = DateUtil.timer(false); // --------------------------------- // -------这是执行过程 // --------------------------------- timer.interval();// 花费毫秒数 timer.intervalRestart();// 返回花费时间,并重置开始时间 timer.intervalMinute();// 花费分钟数