Find total hours between two Dates

This should work.

long secs = (this.endDate.getTime() - this.startDate.getTime()) / 1000;
int hours = secs / 3600;    
secs = secs % 3600;
int mins = secs / 60;
secs = secs % 60;

Here's how it works with Joda time:

DateTime startTime, endTime;
Period p = new Period(startTime, endTime);
int hours = p.getHours();
int minutes = p.getMinutes();

You could format with Joda's formatters, e.g., PeriodFormat, but I'd suggest using Java's. See this question for more details.


Here's simple way:

private static int hoursDifference(Date date1, Date date2) {

    final int MILLI_TO_HOUR = 1000 * 60 * 60;
    return (int) (date1.getTime() - date2.getTime()) / MILLI_TO_HOUR;
}