java - how to get exact month difference in Joda-Time after adding no.of days -
i'm trying find no.of months between 2 dates. help appreciated. i'm using joda-time below example.
datemidnight start = new datemidnight(new date()); datemidnight dtenddate = start.plusdays(11); //adding days int months = months.monthsbetween(start, dtenddate).getmonths(); system.out.println("months between " + start.tostring("yyyy-mm-dd") + " , " + dtenddate.tostring("yyyy-mm-dd") + " = " + months + " month(s)");
the above code returning '0' 2013-feb-18 2013-mar-01
i can't add together +1 months need find difference in same month, need find difference between 2 elapsed periods i.e between '2012-dec-04' , '2013-jan-06' should homecoming -1;
scenario1: date1: 2013-02-18 date1.plusdays(11); date2: 2013-03-01 output : 0 month(s) //but need 1 month scenario2: date1: 2013-02-18 date1.plusdays(1); date2: 2013-02-19 output : 0 month(s) //returns correctly need scenario3: date1: 2013-03-18 date2: 2013-02-19 output should : -1 month(s)
set day of month 1
both dates before comparing:
startmodified = start.withdayofmonth(1); endmodified = end.withdayofmonth(1); int months = months.monthsbetween(startmodified, endmodified).getmonths();
java jodatime
No comments:
Post a Comment