Java 检查两个日期期间是否重叠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18938152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:41:09  来源:igfitidea点击:

Check if two date periods overlap

javaflowchart

提问by Khaled Lela

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

  • My flow chart I assume "<>=" operators is valid for comparing.

    boolean isOverLaped(Date start1,Date end1,Date start2,Date end2) {
        if (start1>=end2 && end2>=start2 && start2>=end2) {
            return false;
        } else {
            return true;
        }
    }
    
  • Any Suggestion will be appreciated.
  • 我有两个日期范围, (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;
        }
    }
    
  • 任何建议将不胜感激。

采纳答案by Sotirios Delimanolis

You can use Joda-Timefor this.

您可以为此使用Joda-Time

It provides the class Intervalwhich specifies a start and end instants and can check for overlaps with overlaps(Interval).

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

Something like

就像是

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 ) );

prints

印刷

true

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

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

回答by Dawood ibn Kareem

boolean overlap(Date start1, Date end1, Date start2, Date end2){
    return start1.getTime() <= end2.getTime() && start2.getTime() <= end1.getTime(); 
}

回答by Bassam Qarib

    //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;
        }

回答by wjr

You have two intervals, i1 and i2. There are six cases for how the intervals can be temporally related (at least in a Newtonian world view) but only two are important: if i1 is entirely before i2 or i1 is entirely after i2; otherwise the two intervals are overlapping (the other four cases are i1 contains i2, i2 contains i1, i1 contains the start of i2 and i1 contains the end of i2). Assume i1 and i2 are of type Interval that have Date fields beginTime and endTime. The function then is (note, the assumption here is that if i1 starts at the same time i2 ends, or vice versa, we don't consider that an overlap and we assme for a given interval endTime.before(beginTime) is false):

你有两个区间,i1 和 i2。间隔如何在时间上相关有六种情况(至少在牛顿世界观中),但只有两种情况很重要:如果 i1 完全在 i2 之前或 i1 完全在 i2 之后;否则两个区间是重叠的(其他四种情况是 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);
}

In the original question, you specify DateTime instead of Date. In java, Date has both date and time. This is in contrast to sql where Date does not have a time element while DateTime does. That is a point of confusion that I stumbled across when I first started using sql after having done only java for many years. Anyway, I hope this explanation is helpful.

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