我目前正在考虑从JodaTime迁移到java8 apis。
大部分都没问题,但有一件事让我失望。我需要计算在“办公时间”内工作班次的人数。
使用JodaTime,我利用Instant类进行计算并获得成功:
DateTime in = new DateTime(inTime);
DateTime out = new DateTime(outTime);
Interval officeHours = new Interval(in.withTime(whstart.getHours(),
whstart.getMinutes(), 0, 0),
in.withTime(whend.getHours(), whend.getMinutes(), 0, 0));
Interval shift = new Interval(in, out);
Interval overlap = officeHours.overlap(shift);
officeHoursSeconds = overlap != null ?
overlap.toDuration().getStandardSeconds() : 0;
但是,由于java8中似乎缺少Interval类,因此我最好将这个逻辑移到java8 api上。
我当然可以继续使用Jodatime,但由于它是EOL'ed,我想尝试完全删除它。
指针非常感谢。
答案 0 :(得分:2)
Joda-class Interval
尚未被Java-8 所取代。我主要看三种方式:
a)编写自己的间隔类(查看源代码可以帮助)
b)使用我的lib Time4J和类MomentInterval和方法findIntersection(...),它产生一个可选的交集区间。存在转换方法为Java-8类,如moment.toTemporalAccessor()
,产生java.time.Instant
。
c)使用lib Threeten-Extra,它具有同名Interval的类及其方法intersection(...)。在这里你必须首先检查是否存在交集,否则将抛出异常。