title: i have set of date ranges, i need to get the combined date range ,if any of the date overlaps in java
body: <p>I have set of date ranges, I need to get the combined date range if any of the dates overlap in Java.</p>
  
  <p>Given three sets of date ranges, if any of the dates overlap with another range of dates need to be combined.</p>
  
  <p><strong>Example:</strong></p>
  
  <pre><code>20170101-20170331
  20170101-20170430
  20170430-20170501
  </code></pre>
  
  <p><strong>Expected result is:</strong></p>
  
  <pre><code>20170101-20170430
  20170430-20170501
  </code></pre>
  
  <p>I have all the dates in String Variable. Can please any one help me to how to write the code for that.  I have pasted below my code.</p>
  
  <p>I want to achieve the expected results. I couldn't find out how I need to modify this code. I am a beginner, please help me to do that. I have got this sample program from StackOverflow.</p>
  
  <pre><code>package com.kkkkk.Combine;
  
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Date;
  
  public class Ideone {
      public static void main(String[] args) throws java.lang.Exception {
          ArrayList&lt;Interval&gt; x = new ArrayList&lt;&gt;();
          x.add(new Interval("20170430", "20170501")); // "20170101", "20170430"
          x.add(new Interval("20170101", "20170430"));// 20170101-20170430
                  x.add(new Interval("20170101", "20170331"));
                  x = merge(x);
  
          for (Interval i1 : x) {
              System.out.println(i1.getStartDate() + " " + i1.getEndDate());
  
          }
  
      }
  
      public static ArrayList&lt;Interval&gt; merge(ArrayList&lt;Interval&gt; intervals) {
  
  
          if (intervals.size() == 0 || intervals.size() == 1)
              return intervals;
  
          ArrayList&lt;Interval&gt; result = new ArrayList&lt;Interval&gt;();
  
          Collections.sort(intervals, new IntervalComparator());
  
  
          System.out.println("intervals ggggg\n" + intervals + "\n");
  
          Interval first = intervals.get(0);
  
          String start = first.getStartDate();
          String end = first.getEndDate();
          Date startDateF = null;
          Date endDateF = null;
  
          try {
              SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
              startDateF = sdf.parse(start);
              endDateF = sdf.parse(end);
  
  
              // ArrayList&lt;Interval&gt; result = new ArrayList&lt;Interval&gt;();
  
              for (int i = 1; i &lt; intervals.size(); i++) {
  
                  Interval current = intervals.get(i);
  
                  Date currentEndDate = sdf.parse(current.getEndDate());
                  Date currentStartDate = sdf.parse(current.getStartDate());
  
                  // if ((current.getStartDate().after(endDateF)) ||
                                  Date d1 = minDate(endDateF, currentStartDate);
  
                                  if ((currentStartDate).compareTo(endDateF) &lt;= 0) {
                      endDateF = maxDate(currentEndDate, endDateF);
  
                                                          } else {
  
                                                              result.add(new Interval(start, (sdf.format(endDateF))));
  
                      // start = current.start;
                      // end = current.end;
  
                      start = sdf.format(currentStartDate);
                      endDateF = (currentEndDate);
  
  
  
      enter code here
  
                  }
  
              }
              result.add(new Interval(start, end));
              // result.add(new Interval(start, (sdf.format(endDateF))));
          }
  
          catch (ParseException ex) {
              ex.printStackTrace();
          }
  
          // result.add(new Interval(start, end));
  
          return result;
          // return intervals;
  
      }
  
      public static Date minDate(Date date1, Date date2) {
          // if date1 before date2 then return date1 else return date2
  
          return date1.before(date2) ? date1 : date2;
      }
  
      /**
       * find Max Dates
       * 
       * @param date1
       * @param date2
       * @return
       */
      public static Date maxDate(Date date1, Date date2) {
          // if date1 after date2 then return date1 else return date2
          System.out.println("date max");
          return date1.after(date2) ? date1 : date2;
      }
  }
  </code></pre>