I am working on dates in Java. I am finding out the difference between two dates.
public static void main(String[] args) {
final Date futDate = new GregorianCalendar(2012, 8, 15, 0, 0, 0).getTime();
final Date currentDate = new GregorianCalendar().getTime();
long diff = Math.round((futDate.getTime() - currentDate.getTime()) / 1000);
System.out.println(diff / 86400 + " days");
System.out.println((diff % 86400) / 3600 + " hrs");
System.out.println(((diff % 86400) % 3600) / 60 + " mins");
System.out.println((((diff % 86400) % 3600) % 60) % 60 + " secs");
}
Output:
31 days
8 hrs
37 mins
30 secs
Even though the date difference is less than a day, the output is more than 31 days.
8 isn’t current month number 🙂
months are numbered from 0
Java’s dates are painful, so I suggest take a look at http://joda-time.sourceforge.net/