Monday, May 5, 2014

Did you know java TimeUnit class

I am not sure why Date din't do it, Calendar didn't do it and Apache DateUtils didn't do it?

Though every other product in every other company would need date diff !!!

I was introduced to TimeUnit through a retry wrapper class where I had to specify retry time using TimeUnit. So no longer we need a variable like private static final long numberOfSecondsInMinutes  = 60; :)

Date diff is a two liner with this class

1. Get the diff in millis
long diff = date1.getTime() - date2.getTime();

2. TimeUnit.MILLISECONDS.toDays(diff);

nJoy

1 comment:

ದತ್ತ said...

http://joda-time.sourceforge.net/apidocs/org/joda/time/base/AbstractInterval.html#toPeriod(org.joda.time.PeriodType)

Joda time library is the most flexible one I have seen so far..