datetime - Find the difference between two dates (Inclusive of start and end date) in Java -
this question has answer here:
- calculating difference in dates in java 7 answers
i need find difference between 2 dates in java , difference should inclusive of start , end date. tried using below piece of code not including start , end date.
long diffdays = days.daysbetween(new datetime(startdate), new datetime(enddate)).getdays(); is there utility method achieve this?
if you're not using library 1 of method:
public static map<timeunit,long> computediff(date date1, date date2) { long diffinmillies = date2.gettime() - date1.gettime(); list<timeunit> units = new arraylist<timeunit>(enumset.allof(timeunit.class)); collections.reverse(units); map<timeunit,long> result = new linkedhashmap<timeunit,long>(); long milliesrest = diffinmillies; ( timeunit unit : units ) { long diff = unit.convert(milliesrest,timeunit.milliseconds); long diffinmilliesforunit = unit.tomillis(diff); milliesrest = milliesrest - diffinmilliesforunit; result.put(unit,diff); } return result; } or else use joda
Comments
Post a Comment