[英]Joda DateTime Overlap and Filtering
I have two Intervals say, 我有两个间隔说,
// for simplicity, I present here only the hours
val interval1 = (08, 20)
val interval2 = (00, 10)
Now, when I do a overlap between the two intervals, I get 现在,当我在两个间隔之间进行重叠时,我得到
val overlap = interval.overlap(interval2) // gives me (08, 10)
Is there a method in joda-time that I can use to get the non overlapping time periods? 在joda-time中是否有一种方法可以用来获取非重叠时间段? I need to get (10, 20)
我需要得到(10,20)
I tried using the gap method: 我尝试使用间隙方法:
scala> val a = new Interval(DateTime.parse("2000-10-12T00:00:00"), DateTime.parse("2000-10-12T20:00:00"))
scala> val b = new Interval(DateTime.parse("2000-09-12T00:00:00"), DateTime.parse("2000-10-12T12:00:00"))
a.gap(b) // returns null
b.gap(a) // returns null
Looks like there is nothing in the library that offers this. 看起来库中没有提供此功能的东西。 So I came up with my own implementation which is like this:
所以我想出了自己的实现,如下所示:
private def splitIntervals(interval1: Interval, interval2: Interval) = {
Option(interval1.overlap(interval2)) match {
case Some(overlap) => {
if (overlap.equals(interval1)) Seq(interval1)
else if (overlap.equals(interval2))
Seq(overlap) :+
new Interval(interval1.getStart, overlap.getStart) :+
new Interval(overlap.getEnd, interval1.getEnd)
else if (overlap.getStart.equals(interval1.getStart))
Seq(overlap) :+
new Interval(overlap.getEnd, interval1.getEnd)
else Seq(overlap) :+
new Interval(interval1.getStart, overlap.getEnd)
}
case _ => Seq(interval1)
}
}
It covers all the possible edge cases! 它涵盖了所有可能的边缘情况!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.