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.
public class DateRange {
public LocalDate start , stop ;
public DateRange ( LocalDate start , LocalDate stop ) {
if(stop.isBefore(start)){
throw new IllegalArgumentException ("The stop date is before the start date." );
}
this.start = start;
this.stop = stop;
}
@Override
public String toString () {
return "DateRange{ " + "start=" + start + ", stop=" + stop + " }";
}
}
Instantiate objects of that class, and collect.
List<DateRange> ranges = new ArrayList<> ( 3 );
ranges.add ( new DateRange ( LocalDate.of ( 2017 , Month.JANUARY , 17 ) , LocalDate.of ( 2017 , Month.MARCH , 7 ) ) );
ranges.add ( new DateRange ( LocalDate.of ( 2017 , Month.FEBRUARY , 12 ) , LocalDate.of ( 2017 , Month.FEBRUARY , 16 ) ) );
ranges.add ( new DateRange ( LocalDate.of ( 2017 , Month.FEBRUARY , 14 ) , LocalDate.of ( 2017 , Month.MARCH , 25 ) ) );
System.out.println ( "ranges: " + ranges );
Extract each start and each stop, collecting into a List
.
// Intersect and combine to create a sequence of new DateRange objects.
// Collect each start & stop as individual `LocalDate` objects.
List<LocalDate> dates = new ArrayList<> ( ranges.size () * 2 );
for ( DateRange range : ranges ) {
dates.add ( range.start );
dates.add ( range.stop );
}
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.
// Sort the collection of dates.
Collections.sort ( dates );
Loop the sorted dates, using each as a start date and the following date as the stop.
// Loop the sorted dates, creating DateRange objects as we go.
List<DateRange> rangesOutput = new ArrayList<> ( dates.size () ); // Not an exact initial capacity but good enough.
for ( int i = 1 ; i < dates.size () ; i ++ ) {
LocalDate start = dates.get ( i - 1 ); // Subtract one for silly index counting.
LocalDate stop = dates.get ( i + 1 - 1 ); // Subtract one for silly index counting. Or use ( i ) instead.
if ( ! start.equals ( stop ) ) { // If not equal, proceed. (If equal, ignore and move on to next loop.)
DateRange range = new DateRange ( start , stop );
rangesOutput.add ( range );
}
}
System.out.println ( "rangesOutput: " + rangesOutput );
When run.
ranges: [DateRange{ start=2017-01-17, stop=2017-03-07 }, DateRange{ start=2017-02-12, stop=2017-02-16 }, DateRange{ start=2017-02-14, stop=2017-03-25 }]
rangesOutput: [DateRange{ start=2017-01-17, stop=2017-02-12 }, DateRange{ start=2017-02-12, stop=2017-02-14 }, DateRange{ start=2017-02-14, stop=2017-02-16 }, DateRange{ start=2017-02-16, stop=2017-03-07 }, DateRange{ start=2017-03-07, stop=2017-03-25 }]
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?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
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.