1
  • (start1,end1):::>>date1 && (start2,end2):::>>date2 という 2 つの日付範囲があります。
  • 2 つの日付が重複しているかどうかを確認したい。

  • 私のフローチャートは、「<>=」演算子が比較に有効であると想定しています。

    boolean isOverLaped(Date start1,Date end1,Date start2,Date end2) {
        if (start1>=end2 && end2>=start2 && start2>=end2) {
            return false;
        } else {
            return true;
        }
    }
    
  • 任意の提案をいただければ幸いです。
4

4 に答える 4

2
boolean overlap(Date start1, Date end1, Date start2, Date end2){
    return start1.getTime() <= end2.getTime() && start2.getTime() <= end1.getTime(); 
}
于 2013-09-21T22:07:36.100 に答える
0
    //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;
        }
于 2014-01-16T12:43:37.967 に答える
0

i1 と i2 の 2 つの間隔があります。間隔が時間的にどのように関連付けられるかについては 6 つのケースがありますが (少なくともニュートンの世界観では)、重要なのは 2 つだけです。それ以外の場合、2 つの間隔は重複しています (他の 4 つのケースでは、i1 に i2 が含まれ、i2 に i1 が含まれ、i1 に i2 の開始が含まれ、i1 に i2 の終了が含まれます)。i1 と i2 は、日付フィールド beginTime と endTime を持つ Interval 型であると仮定します。関数は次のようになります (ここでの仮定は、i1 が i2 の終了と同時に開始する場合、またはその逆の場合、オーバーラップとは見なされず、特定の間隔 endTime.before(beginTime) が false であると見なされます) :

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

元の質問では、Date の代わりに DateTime を指定しています。Java では、Date には日付と時刻の両方があります。これは、DateTime には時間要素があり、Date には時間要素がない SQL とは対照的です。これは、私が何年もの間 Java だけを使ってきた後、最初に SQL を使い始めたときに遭遇した混乱のポイントです。とにかく、この説明が役に立てば幸いです。

于 2014-08-17T15:58:50.820 に答える