首页 > 解决方案 > 从基本时间间隔中减去一个间隔后如何获取剩余间隔列表

问题描述

java在从基本时间间隔中删除子时间间隔后,是否有任何方法可以列出剩余的时间间隔。

示例:考虑存在 0 到 24 小时的时间间隔,如果我从10 AM到删除时间12AM,那么剩余的时间间隔应该是0-1012-24

在这种情况下,基本间隔是 0-24 小时。

标签: javajodatime

解决方案


I did not find method for this in Joda library nor in SDK.

Basically interval is defined as a start and end timestamp. Method for substracting could have this logic:

Base interval (s1,e1)

Subsctracted interval (s2,e2)

If s1<s2 and e2<e1 then complement intervals are (s1, s2) and (e2, e1)

If s2<s1 and e2<e1 then complement is (e2, e1)

If s1<s2 and e1<e2 then complement is (s1, s2)

If base is inside substracted then complement is null

If no intersection result is base interval.

enter image description here


推荐阅读