【问题标题】:Check if two date periods overlap [duplicate]检查两个日期期间是否重叠[重复]
【发布时间】:2013-09-27 02:29:43
【问题描述】:
  • 我有两个日期范围,(start1,end1):::>>date1 && (start2,end2):::>>date2。
  • 我想检查两个日期是否重叠。

  • 我的流程图我假设“=”运算符可用于比较

    boolean isOverLaped(Date start1,Date end1,Date start2,Date end2) {
        if (start1>=end2 && end2>=start2 && start2>=end2) {
            return false;
        } else {
            return true;
        }
    }
    
  • 我们将不胜感激任何建议。

【问题讨论】:

标签: java flowchart


【解决方案1】:

您可以为此使用Joda-Time

它提供了Interval 类,它指定了开始和结束时刻,并且可以检查与overlaps(Interval) 的重叠。

有点像

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

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

【讨论】:

  • 感谢您的解决方法,但我们只想使用该运算符。并假设未排序的日期范围。
  • @KhaledLela 如果参数的顺序不正确,Interval 类将引发异常。您可以利用它来正确实例化(或使用日期的毫秒值决定哪个先出现)。 JodaTime 解决方案提供的代码比使用>< 更简洁。
  • 要将此示例代码与现有 java.util.Date 对象一起使用,只需将每个 Date 实例传递给 Joda-Time DateTime 构造函数:DateTime dateTimeStart1 = new DateTime( start1 );
  • 如果两个区间相等,重叠将返回 false!小心!
【解决方案2】:
boolean overlap(Date start1, Date end1, Date start2, Date end2){
    return start1.getTime() <= end2.getTime() && start2.getTime() <= end1.getTime(); 
}

【讨论】:

  • 除了start1 被声明为Date 类型,其他也是。
  • 哦,没看到。现已更正。
  • 日期时间段没有排序怎么办,比如“end1可能在start1之前”。
  • 那么您是说您的日期范围甚至可能无效?如果你也需要验证,我会写一个单独的方法。
【解决方案3】:
    //the inserted interval date is start with fromDate1 and end with toDate1
    //the date you want to compare with start with fromDate2 and end with toDate2

if ((int)(toDate1 - fromDate2).TotalDays < 0 )
        { return true;}
else
{    
 Response.Write("<script>alert('there is an intersection between the inserted date interval and the one you want to compare with')</script>");
            return false;
        }

if ((int)(fromDate1 - toDate2).TotalDays > 0 )
        { return true;}
else
{    
 Response.Write("<script>alert('there is an intersection between the inserted date interval and the one you want to compare with')</script>");
            return false;
        }

【讨论】:

  • 您的代码是用 Java 以外的其他语言编写的吗?这个问题是针对 Java 的。在 Java 中,我们不能使用减号来减去一对 Date 实例。而且我不认识.TotalDays
【解决方案4】:

你有两个区间,i1 和 i2。有六种情况可以说明区间如何在时间上相关(至少在牛顿世界观中),但只有两种是重要的:如果 i1 完全在 i2 之前或 i1 完全在 i2 之后;否则两个区间是重叠的(其他四种情况是 i1 包含 i2,i2 包含 i1,i1 包含 i2 的开头,i1 包含 i2 的结尾)。假设 i1 和 i2 是具有日期字段 beginTime 和 endTime 的 Interval 类型。然后函数是(注意,这里的假设是,如果 i1 在 i2 结束的同时开始,反之亦然,我们不认为重叠,我们假设给定间隔 endTime.before(beginTime) 为假) :

boolean isOverlapped(Interval i1, Interval i2) {
    return i1.endTime.before(i2.beginTime) || i1.beginTime.after(i2.endTime);
}

在原始问题中,您指定了 DateTime 而不是 Date。在 java 中,Date 有日期和时间。这与 sql 形成对比,其中 Date 没有时间元素,而 DateTime 有。这是我在多年只使用 java 之后第一次开始使用 sql 时偶然发现的一个困惑点。无论如何,我希望这个解释会有所帮助。

【讨论】:

  • 这是不正确的。如果区间 i1 完全在区间 i2 之前,这仍然会返回 true。
  • 这真的错了。正确的比较是:!(beginPeriod1.isAfter(endPeriod2) || beginPeriod2.isAfter(endPeriod1))
猜你喜欢
  • 2019-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-16
  • 1970-01-01
相关资源
最近更新 更多