java - Find the number of months and day between date range -


i have start date , end date in database below.

start date:01/06/2014 end date:30/06/2014

start date:01/07/2014 end date:30/09/2014

start date:01/10/2014 end date:31/03/2015

if enter date range

start date 02/06/2014
end date 01/02/2015

the output has be.

28 days, in 1st slab date range  2 months, 29 days, in 2nd slab date range  4 months in 3rd slab date range 

how achieve in java.

thanks in advance.

this question difficult answer accurately. believe want,

// minimum of number of dates. private static date getminimum(date... dates) {   if (dates == null)     return null;   date min = dates[0];   (date d : dates) {     if (d.compareto(min) < 0) {       min = d;     }   }   return min; }  // maximum of number of dates. private static date getmaximum(date... dates) {   if (dates == null)     return null;   date max = dates[0];   (date d : dates) {     if (d.compareto(max) > 0) {       max = d;     }   }   return max; }  public static string getdatediff(date startdate,     date enddate) {   stringbuilder sb = new stringbuilder();   calendar start = calendar.getinstance();   start.settime(getminimum(startdate, enddate));   calendar end = calendar.getinstance();   end.settime(getmaximum(startdate, enddate));   if (start.compareto(end) < 0) {     int monthcount = 0;     int daycount = 0;     while (start.compareto(end) < 0) {       start.add(calendar.month, 1);       if (start.compareto(end) < 0) {         monthcount++;       }     }     start = calendar.getinstance();     start.settime(getminimum(startdate, enddate));     start.add(calendar.month, monthcount);     while (start.compareto(end) < 0) {       start.add(calendar.day_of_month, 1);       if (start.compareto(end) < 0) {         daycount++;       }     }     if (monthcount > 0) {       sb.append(string.format("%d months",           monthcount));     }     if (daycount > 0) {       if (sb.length() > 0) {         sb.append(", ");       }       sb.append(string.format("%d days", daycount));     }   } else {     sb.append("0 days");   }    return sb.tostring(); }  public static void main(string[] args) {   string[] input = { "01/06/2014-30/06/2014", //       "01/07/2014-30/09/2014", //       "01/10/2014-31/03/2015", //       "02/06/2014-01/02/2015", };   dateformat df = new simpledateformat("dd/mm/yyyy");   (string str : input) {     string sarr[] = str.split("-");     try {       date start = df.parse(sarr[0]);       date end = df.parse(sarr[1]);       system.out.printf("start: %s, end: %s - diff: %s\n", sarr[0],           sarr[1], getdatediff(start, end));     } catch (parseexception e) {       e.printstacktrace();     }   } } 

the output is

start: 01/06/2014, end: 30/06/2014 - diff: 28 days start: 01/07/2014, end: 30/09/2014 - diff: 2 months, 28 days start: 01/10/2014, end: 31/03/2015 - diff: 5 months, 29 days start: 02/06/2014, end: 01/02/2015 - diff: 7 months, 29 days 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -