检查两个日期时段是否重叠 [英] Check if two date periods overlap

查看:142
本文介绍了检查两个日期时段是否重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 我有两个日期范围,(start1,end1)::: >> date1&& (start2,end2)::: >> date2。

  • 我想检查这两个日期是否已经过了。

  • I have two date ranges, (start1,end1):::>>date1 && (start2,end2):::>>date2 .
  • I want to check if the two dates isOverLaped.

我的流程图我认为<> =运营商有效进行比较

boolean isOverLaped(Date start1,Date end1,Date start2,Date end2) {
    if (start1>=end2 && end2>=start2 && start2>=end2) {
        return false;
    } else {
        return true;
    }
}


  • 任何建议都将不胜感激。

  • 推荐答案

    您可以使用 Joda-Time

    它提供了类 间隔 指定开始和结束时刻,可以检查重叠与重叠(间隔)

    类似

    DateTime now = DateTime.now();
    
    DateTime start1 = now;
    DateTime end1 = now.plusMinutes(1);
    
    DateTime start2 = now.plusSeconds(50);
    DateTime end2 = now.plusMinutes(2);
    
    Interval interval = new Interval( start1, end1 );
    Interval interval2 = new Interval( start2, end2 );
    
    System.out.println( interval.overlaps( interval2 ) );
    

    打印

    true
    

    因为第一个间隔的结束落在第二个间隔的开始和结束之间间隔。

    since the end of the first interval falls between the start and end of the second interval.

    这篇关于检查两个日期时段是否重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆