string - i have set of date ranges, i need to get the combined date range ,if any of the date overlaps in java -


this question has answer here:

i have set of date ranges, need combined date range if of dates overlap in java.

given 3 sets of date ranges, if of dates overlap range of dates need combined.

example:

20170101-20170331 20170101-20170430 20170430-20170501 

expected result is:

20170101-20170430 20170430-20170501 

i have dates in string variable. can please 1 me how write code that. have pasted below code.

i want achieve expected results. couldn't find out how need modify code. beginner, please me that. have got sample program stackoverflow.

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<interval> x = new arraylist<>();         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);          (interval i1 : x) {             system.out.println(i1.getstartdate() + " " + i1.getenddate());          }      }      public static arraylist<interval> merge(arraylist<interval> intervals) {           if (intervals.size() == 0 || intervals.size() == 1)             return intervals;          arraylist<interval> result = new arraylist<interval>();          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<interval> result = new arraylist<interval>();              (int = 1; < 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) <= 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 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 return date1 else return date2         system.out.println("date max");         return date1.after(date2) ? date1 : date2;     } } 

iso 8601

use standard iso 8601 formats when serializing date-time values text. format complies “basic” version of standard, better use full format when possible:

yyyy-mm-dd

use standard format date range, using slash character separator:

yyyy-mm-dd/yyyy-mm-dd

if cannot alter input strings, split string on hyphen. parse each piece localdate. use objects instantiate localdaterange.

localdate ld = localdate.parse( "20170101" , datetimeformatter.basic_iso_date ) ;  

localdaterange

use localdaterange class threeten-extra project extends java.time class functionality. uses standard format when parsing , generating text.

localdaterange range = localdaterange.parse( "2017-01-01/2017-03-31" ) ; 

collect in list<localdaterange>.

to sort, write comparator calls localdaterange::getstart.

compare range see if overlap. if so, combine call union.

if ( range.overlaps( otherrange ) ) {     range = range.union( otherrange ) ; } 

if not overlap, have finished round. store result in list<localdaterange>. start round next range.

lather, rinse, repeat.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -