将右无界时间间隔与 joda-lib 进行比较

发布于 2024-12-05 03:02:59 字数 548 浏览 1 评论 0原文

是否可以确定两个无界区间(一个边界为无穷大的区间)是否重叠?

我已经尝试过这个(以及其他类似的变体):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对不⑦ 2024-12-12 03:03:00

黑客解决方案:

/**
 * Checks if two (optionally) unbounded intervals overlap each other.
 * @param aBeginn
 * @param aEnde
 * @param bBeginn
 * @param bEnde
 * @return
 */
public boolean checkIfOverlap(LocalDate aBeginn,LocalDate aEnde, LocalDate bBeginn,LocalDate bEnde){
    if(aBeginn == null){
        //set the date to the past if null
        aBeginn = LocalDate.now().minusYears(300);
    }
    if(aEnde == null){
        aEnde = LocalDate.now().plusYears(300);
    }
    if(bBeginn == null){
        bBeginn = LocalDate.now().minusYears(300);
    }
    if(bEnde == null){
        bEnde = LocalDate.now().plusYears(300);
    }

    if(aBeginn != null && aEnde != null && bBeginn != null && bEnde != null){
        Interval intervalA = new Interval(aBeginn.toDateTimeAtStartOfDay(),aEnde.toDateTimeAtStartOfDay());
        Interval intervalB = new Interval(bBeginn.toDateTimeAtStartOfDay(),bEnde.toDateTimeAtStartOfDay());
        if(intervalA.overlaps(intervalB)){
            return true;
        }
    } else{
        return false;
    }
    return false;
}

Hacky solution:

/**
 * Checks if two (optionally) unbounded intervals overlap each other.
 * @param aBeginn
 * @param aEnde
 * @param bBeginn
 * @param bEnde
 * @return
 */
public boolean checkIfOverlap(LocalDate aBeginn,LocalDate aEnde, LocalDate bBeginn,LocalDate bEnde){
    if(aBeginn == null){
        //set the date to the past if null
        aBeginn = LocalDate.now().minusYears(300);
    }
    if(aEnde == null){
        aEnde = LocalDate.now().plusYears(300);
    }
    if(bBeginn == null){
        bBeginn = LocalDate.now().minusYears(300);
    }
    if(bEnde == null){
        bEnde = LocalDate.now().plusYears(300);
    }

    if(aBeginn != null && aEnde != null && bBeginn != null && bEnde != null){
        Interval intervalA = new Interval(aBeginn.toDateTimeAtStartOfDay(),aEnde.toDateTimeAtStartOfDay());
        Interval intervalB = new Interval(bBeginn.toDateTimeAtStartOfDay(),bEnde.toDateTimeAtStartOfDay());
        if(intervalA.overlaps(intervalB)){
            return true;
        }
    } else{
        return false;
    }
    return false;
}
哥,最终变帅啦 2024-12-12 03:03:00

我建议使用 Range (来自 Guava),它应该提供您需要的所有构造选项和比较功能。

Range<DateTime> r1= Range.atLeast(DateTime.now());
Range<DateTime> r2 = Range.atLeast(DateTime.now());
boolean overlap = r1.isConnected(r2) && !r1.intersection(r2).isEmpty();

I'd recommend using a Range<DateTime> (from Guava), which should provide all the construction options and comparison functions you need.

Range<DateTime> r1= Range.atLeast(DateTime.now());
Range<DateTime> r2 = Range.atLeast(DateTime.now());
boolean overlap = r1.isConnected(r2) && !r1.intersection(r2).isEmpty();
堇年纸鸢 2024-12-12 03:02:59

如果两个间隔都以 t = -∞ 开始,或者两个间隔都以 t = +∞ 结束,则它们将始终重叠,无论开始日期。

如果区间 A 开始于 t = -∞ 且区间 B 结束于 t = +∞,则它们重叠 <强>当且仅当
A.start> B.开始

If both intervals start at t = -∞ , or if both intervals end at t = +∞, they will always overlap, regardless of the start date.

If interval A starts at t = -∞ and interval B ends at t = +∞, they overlap iff
A.start > B.start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文