21人参与 • 2026-01-20 • ar
自从认识了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计算日期间隔就不用借助其他工具类了,他自身就有函数可以实现这个功能😆。
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对象之间的间隔,也可以使用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 + "天");

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论