I have an input set of date ranges that may overlap. Instead of combining these overlapping date ranges, I want to create new date ranges with adjusted dates, e.g.:
|---------------------–|
|-----|
|--------------–|
should end up in:
|-------|---|-|--------|----|
Is there an efficient way to solve this with Java?
Thanks in advance!
UPDATE: I didn't mention my own approach in my first question, so here it is: I'd simply take the start and the end date of an interval and add it to a sorted set. Afterwards I'd iterate over the set and create new intervals based on re-ordered dates.
To solve such problem, sort your intervals using start date as first criteria and end date as second. This way you can intersect the intervals in a single iteration. If your interval is overlapped by another interval that starts no sooner, then its successor in the sorted order should be an overlapping interval and so on.
You could use Guava's Range support. Haven't used it with Date objects but it could work. Combined with RangeSet you could add all date ranges and then check if a Date is in the ranges, get the complete range, etc.
The basic idea:
Keep track of
startIntervals - endIntervals
and whenever this number is 0, there should be no interval in that range.Using my time library Time4J, following convenient solution is possible without much brainstorming about the real implementation:
Output:
The main adjustment after defining and gathering all the intervals is just calling withSplits() on the interval collection.
Another advantage is the option to slightly adjust the code such that working with other types like the class
java.time.Instant
from Java-8 or the built-in Time4J-types likeMoment
,PlainDate
,PlainTimestamp
etc. is easily possible, too.Using java.time
Let's use the modern java.time classes.
The
LocalDate
class represents a date-only value without time-of-day and without time zone.Create a little class
DateRange
to hold the stop and start dates.Instantiate objects of that class, and collect.
Extract each start and each stop, collecting into a
List
.Sort the dates. As shown in the Question, the various ranges may overlap with one another. We need them in chronological order so as to make new abutting date ranges.
Loop the sorted dates, using each as a start date and the following date as the stop.
When run.
See this code live at IdeOne.com.
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.
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.