fork(5) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.time.*;
  8. import java.time.temporal.*;
  9. import java.time.format.*;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16.  
  17. List<DateRange> ranges = new ArrayList<> ( 3 );
  18. ranges.add ( new DateRange ( LocalDate.of ( 2017 , Month.JANUARY , 17 ) , LocalDate.of ( 2017 , Month.MARCH , 7 ) ) );
  19. ranges.add ( new DateRange ( LocalDate.of ( 2017 , Month.FEBRUARY , 12 ) , LocalDate.of ( 2017 , Month.FEBRUARY , 16 ) ) );
  20. ranges.add ( new DateRange ( LocalDate.of ( 2017 , Month.FEBRUARY , 14 ) , LocalDate.of ( 2017 , Month.MARCH , 25 ) ) );
  21.  
  22. System.out.println ( "ranges: " + ranges );
  23.  
  24. // Intersect and combine to create a sequence of new DateRange objects.
  25. // Collect each start & stop as individual `LocalDate` objects.
  26. List<LocalDate> dates = new ArrayList<> ( ranges.size () * 2 );
  27. for ( DateRange range : ranges ) {
  28. dates.add ( range.start );
  29. dates.add ( range.stop );
  30. }
  31.  
  32. // Sort the collection of dates.
  33. Collections.sort ( dates );
  34.  
  35. // Loop the sorted dates, creating DateRange objects as we go.
  36. List<DateRange> rangesOutput = new ArrayList<> ( dates.size () ); // Not an exact initial capacity but good enough.
  37. for ( int i = 1 ; i < dates.size () ; i ++ ) {
  38. LocalDate start = dates.get ( i - 1 ); // Subtract one for silly index counting.
  39. LocalDate stop = dates.get ( i + 1 - 1 ); // Subtract one for silly index counting. Or use ( i ) instead.
  40. if ( ! start.equals ( stop ) ) { // If not equal, proceed. (If equal, ignore and move on to next loop.)
  41. DateRange range = new DateRange ( start , stop );
  42. rangesOutput.add ( range );
  43. }
  44. }
  45. System.out.println ( "rangesOutput: " + rangesOutput );
  46.  
  47. }
  48. }
  49.  
  50. class DateRange {
  51.  
  52. public LocalDate start, stop;
  53.  
  54. public DateRange ( LocalDate start , LocalDate stop ) {
  55. if ( stop.isBefore ( start ) ) {
  56. throw new IllegalArgumentException ( "The stop date is before the start date." );
  57. }
  58. this.start = start;
  59. this.stop = stop;
  60. }
  61.  
  62. @Override
  63. public String toString () {
  64. return "DateRange{ " + "start=" + start + ", stop=" + stop + " }";
  65. }
  66.  
  67. }
  68.  
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
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 }]