I have a method which uses following logic to calculate difference between days.
long diff = milliseconds2 - milliseconds1;
long diffDays = diff / (24 * 60 *
Try this:
DateFormat format = DateFormat.getDateTimeInstance();
Date completeDate=null;
Date postedDate=null;
try
{
completeDate = format.parse("18-May-09 11:30:57");
postedDate = format.parse("11-May-09 10:46:37");
long res = completeDate.getTime() - postedDate.getTime();
System.out.println("postedDate: " + postedDate);
System.out.println("completeDate: " + completeDate);
System.out.println("result: " + res + '\n' + "minutes: " + (double) res / (60*1000) + '\n'
+ "hours: " + (double) res / (60*60*1000) + '\n' + "days: " + (double) res / (24*60*60*1000));
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
If you want days to be an integer then just remove casting to double. HTH
It's a well worn line, but for Dates use JodaTime.
Here's how to calculate date intervals using JodaTime.
Days days = Days.daysBetween(new DateTime(millis1), new DateTime(millis2));
int daysBetweenDates = days.getDays();
This assumes times are in UTC or GMT.
long day1 = milliseconds1/ (24 * 60 * 60 * 1000);
long day2 = milliseconds2/ (24 * 60 * 60 * 1000);
// the difference plus one to be inclusive of all days
long intervalDays = day2 - day1 + 1;