自从认识了localdatetime之后,使用的频率越来越高了,使用多了就不可避免的涉及到日期的比较、加减以及计算日期间隔这些操作。
但是我发现自己好像不会,那就网上查资料,整呗。
localdatetime
要计算两个localdatetime对象之间的间隔,可以使用java.time.duration类。
可以很方便的获取两个时间之间的间隔。
datetimeformatter formatter = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss");
localdatetime starttime = localdatetime.of(2024, 7, 26, 5, 22, 56);
localdatetime endtime = localdatetime.of(2024, 7, 26, 20, 23, 15);
duration duration = duration.between(starttime, endtime);
system.out.println(starttime.format(formatter) + "\n" + endtime.format(formatter));
system.out.println("间隔天数:" + duration.todays());
system.out.println("间隔小时: " + duration.tohours());
system.out.println("间隔分钟: " + duration.tominutes());
system.out.println("间隔秒数: " + duration.toseconds());

通过运行程序发现一个问题,就是获取的天数、小时数、分钟数都是取整的。
如果需要精确到小数,可以获取秒数自己计算。
datetimeformatter formatter = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss");
localdatetime starttime = localdatetime.of(2024, 7, 26, 5, 22, 56);
localdatetime endtime = localdatetime.of(2024, 7, 26, 20, 23, 15);
duration duration = duration.between(starttime, endtime);
system.out.println(starttime.format(formatter) + "\n" + endtime.format(formatter));
system.out.println("间隔天数:" + duration.todays());
system.out.println("间隔小时: " + duration.tohours());
system.out.println("间隔分钟: " + duration.tominutes());
system.out.println("间隔秒数: " + duration.toseconds());
// 自己计算
bigdecimal divisor = bigdecimal.valueof(60);
bigdecimal minutes = bigdecimal.valueof(duration.toseconds()).divide(divisor, 2, roundingmode.half_up);
bigdecimal hours = bigdecimal.valueof(duration.toseconds()).divide(divisor.multiply(divisor), 2, roundingmode.half_up);
bigdecimal day = bigdecimal.valueof(duration.toseconds()).divide(divisor.multiply(divisor).multiply(bigdecimal.valueof(24)), 2, roundingmode.half_up);
system.out.println("间隔时间: " + duration.toseconds() + "秒, " + minutes + "分钟, " + hours + "小时, " + day + "天");

计算的精度需要自己把控。
localdate
localdate计算日期间隔就不用借助其他工具类了,他自身就有函数可以实现这个功能😆。
datetimeformatter formatter = datetimeformatter.ofpattern("yyyy-mm-dd");
localdate startdate = localdate.of(2024, 11, 23);
localdate enddate = localdate.of(2024, 11, 26);
long days = startdate.until(enddate, chronounit.days);
system.out.println(startdate.format(formatter) + "\n" + enddate.format(formatter));
system.out.println("间隔天数:" + days);

- 基本算到天数就可以了,每月(年)的天数都是不固定的,也不好用天数来计算月(年)数。
- 有时候需要判断两个时间的时间间隔是否大于一个月(年),可以通过开始日期加一月(年)然后和结束日期比较大小的方式来实现。
还有一种算法,可以这样:
datetimeformatter formatter = datetimeformatter.ofpattern("yyyy-mm-dd");
localdate startdate = localdate.of(2024, 9, 23);
localdate enddate = localdate.of(2024, 11, 26);
period until = startdate.until(enddate);
system.out.println(startdate.format(formatter) + "\n" + enddate.format(formatter));
system.out.println("间隔年数:" + until.getyears());
system.out.println("间隔月数:" + until.getmonths());
system.out.println("间隔天数:" + until.getdays());

例二:

这里的意思是开始时间和结束时间之间相差0年1个月28天,连起来看才更合常理,分开来用的的场景不多。
localtime
要计算两个localtime对象之间的间隔,也可以使用java.time.duration类。
可以参考localdatetime。
datetimeformatter formatter = datetimeformatter.ofpattern("hh:mm:ss");
localtime starttime = localtime.of(4, 7, 25);
localtime endtime = localtime.of(20, 7, 26);
duration duration = duration.between(starttime, endtime);
system.out.println(starttime.format(formatter) + "\n" + endtime.format(formatter));
system.out.println("间隔天数:" + duration.todays());
system.out.println("间隔小时: " + duration.tohours());
system.out.println("间隔分钟: " + duration.tominutes());
system.out.println("间隔秒数: " + duration.toseconds());
// 自己计算
bigdecimal divisor = bigdecimal.valueof(60);
bigdecimal minutes = bigdecimal.valueof(duration.toseconds()).divide(divisor, 2, roundingmode.half_up);
bigdecimal hours = bigdecimal.valueof(duration.toseconds()).divide(divisor.multiply(divisor), 2, roundingmode.half_up);
bigdecimal day = bigdecimal.valueof(duration.toseconds()).divide(divisor.multiply(divisor).multiply(bigdecimal.valueof(24)), 2, roundingmode.half_up);
system.out.println("间隔时间: " + duration.toseconds() + "秒, " + minutes + "分钟, " + hours + "小时, " + day + "天");

总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论