Get difference between two dates in months using Java -


this question has answer here:

i need difference between 2 dates using java. need result in months.

example:

startdate = 2013-04-03 enddate = 2013-05-03 result should 1

if interval

startdate = 2013-04-03 enddate = 2014-04-03 result should 12

using following code can results in days. how can in months?

date startdate = new date(2013,2,2); date enddate = new date(2013,3,2); int difindays = (int) ((enddate.gettime() - startdate.gettime())/(1000*60*60*24)); 

if can't use jodatime, can following:

calendar startcalendar = new gregoriancalendar(); startcalendar.settime(startdate); calendar endcalendar = new gregoriancalendar(); endcalendar.settime(enddate);  int diffyear = endcalendar.get(calendar.year) - startcalendar.get(calendar.year); int diffmonth = diffyear * 12 + endcalendar.get(calendar.month) - startcalendar.get(calendar.month); 

note if dates 2013-01-31 , 2013-02-01, distance of 1 month way, may or may not want.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -