1

Scenario 1:

  • R1 = 02/08/2018 - 20/08/2018
  • R2 = 06/08/2018 - 29/08/2018

     R1   |--------------|
     R2          |---------------------|
    

Scenario 2:

  • R1 = 02/08/2018 - 20/08/2018
  • R2 = 31/08/2018 - 16/09/2018

     R1   |--------------|
     R2                     |---------------------|
    

There are two different date ranges, and I need to compare these ranges to find out if they intersect or not. For example, in the first scenario, the ranges intersect and in the second scenario they do not. The dates are in dd/MM/yyyy format.

What is the best way to determine if they intersect or not using Java?

       def R1Initial = SDF.parse(u.effectiveStartDate.text())
       def R1End = SDF.parse(u.mdfSystemEffectiveEndDate.text())
       def R2Initial = SDF.parse(u.effectiveStartDate.text())
       def R2End = SDF.parse(u2.mdfSystemEffectiveEndDate.text())
       def intersects = ?????
Jacob
  • 299
  • 1
  • 18
Steven Diaz
  • 139
  • 12
  • @Jacob Not quite a duplicate, as that Question is language-agnostic. This Question is **language-specific, with Groovy & Java**. With Groovy, we have a specific solution supported by the language, as seen in [the Answer](https://stackoverflow.com/a/51710748/642706) by bdkosher. In Java, we have a specific solution available to us in the *java.time* classes and *ThreeTen-Extra* project, as seen in [my Answer](https://stackoverflow.com/a/51715319/642706). I looked for other Java-specific originals asking about overlapping date ranges, but did not find any. Hence my Answer and my up-votes. – Basil Bourque Aug 06 '18 at 20:43

2 Answers2

4

You can create ranges of LocalDate instances in Groovy 2.5. Ranges (which extend Iterables) have an intersect method, which returns a collection of the overlapping days between two ranges. If the intersection is non-empty, you know that there is overlap:

import java.time.*

def r1 = (LocalDate.of(2018, 8, 2)..LocalDate.of(2018, 8, 20))
def r2 = (LocalDate.of(2018, 8, 6)..LocalDate.of(2018, 8, 29))

def overlap = r1.intersect(r2) // returns a list containing August 6th through August 20th
bdkosher
  • 5,753
  • 2
  • 33
  • 40
0

The Answer by bdkosher looks correct, taking advantage of a nifty feature in Groovy.

org.threeten.extra.LocalDateRange

If doing much work with date-ranges, you may find helpful the LocalDateRange class in the ThreeTen-Extra project.

That class offers several methods for comparison: abuts, contains, encloses, equals, intersection, isBefore, isAfter, isConnected, overlaps, span, and union.

LocalDateRange r1 = 
    LocalDateRange.of( 
        LocalDate.of( 2018, 8 , 2 ) , 
        LocalDate.of( 2018 , 8 , 20 ) 
    ) 
;
LocalDateRange r2 = LocalDateRange.of( LocalDate.of( 2018 , 8 , 6 ) , LocalDate.of( 2018 , 8 , 29 ) ) ;

boolean overlaps = r1.overlaps( r2 ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154