将右无界时间间隔与 joda-lib 进行比较
是否可以确定两个无界区间(一个边界为无穷大的区间)是否重叠?
我已经尝试过这个(以及其他类似的变体):
Instant now = new Instant(new Date().getTime());
Interval i2 = new Interval(now, (ReadableInstant) null);
Interval i1 = new Interval(now, (ReadableInstant) null);
boolean overlapping = i2.overlaps(i1);
但根据文档,使用 null
作为第二个参数意味着“现在”而不是“无穷大”。
编辑:我找到了这个答案 在邮件列表中,所以这对于 Joda 来说似乎是不可能的。我现在正在寻找替代实现。
Is it possible to determine wether two rigth-unbounded intervals (intervals with one boundary at infinity) overlap or not?
I've tried this (and other similar variations):
Instant now = new Instant(new Date().getTime());
Interval i2 = new Interval(now, (ReadableInstant) null);
Interval i1 = new Interval(now, (ReadableInstant) null);
boolean overlapping = i2.overlaps(i1);
But according to the docs, using null
as a second parameter means "now" instead of "infinity".
EDIT: I've found this answer in the mailing list, so it seems to be impossible with Joda. I am now looking for alternative implementations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
黑客解决方案:
Hacky solution:
我建议使用
Range
(来自 Guava),它应该提供您需要的所有构造选项和比较功能。I'd recommend using a
Range<DateTime>
(from Guava), which should provide all the construction options and comparison functions you need.如果两个间隔都以
t = -∞
开始,或者两个间隔都以t = +∞
结束,则它们将始终重叠,无论开始日期。如果区间
A
开始于t = -∞
且区间B
结束于t = +∞
,则它们重叠 <强>当且仅当A.start> B.开始
。If both intervals start at
t = -∞
, or if both intervals end att = +∞
, they will always overlap, regardless of the start date.If interval
A
starts att = -∞
and intervalB
ends att = +∞
, they overlap iffA.start > B.start
.