检查两个日期时段是否重叠

Kha*_*ela 0 java flowchart

  • 我有两个日期范围,(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;
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 任何建议将不胜感激.

Sot*_*lis 11

You can use Joda-Time for this.

It provides the class Interval which specifies a start and end instants and can check for overlaps with 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 ) );
Run Code Online (Sandbox Code Playgroud)

prints

true
Run Code Online (Sandbox Code Playgroud)

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