Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference, in minutes, between two dates

I need to get the number of minutes between two dates. I know Joda is the best to use, but this is for an Android project, so I prefer to use a little external libraries as possible, and the app I'm building doesn't require the calculation to be surgically precise.

However, the code I'm using doesn't seem to be working. I'm trying to get the number of minutes between "11/21/2011 7:00:00 AM" and "11/21/2011 1:00:00 PM" (which should be 360 minutes), but the code I'm using returns 0:

int diff = minutesDiff(GetItemDate("11/21/2011 7:00:00 AM"), GetItemDate("11/21/2011 1:00:00 PM"));

public static Date GetItemDate(final String date)
{
    final Calendar cal = Calendar.getInstance(TimeZone.getDefault());
    final SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
    format.setCalendar(cal);

    try {
        return format.parse(date);
    } catch (ParseException e) {
        return null;
    }
}

public static int minutesDiff(Date earlierDate, Date laterDate)
{
    if( earlierDate == null || laterDate == null ) return 0;

    return (int)((laterDate.getTime()/60000) - (earlierDate.getTime()/60000));
}
like image 508
Kris B Avatar asked Nov 21 '11 18:11

Kris B


People also ask

How do you find the difference in minutes between two dates?

Math. abs is used to be able to use the absolute difference (so new Date('2011/10/09 00:00') - new Date('2011/10/09 12:00') gives the same result). Dividing the result by 1000 gives you the number of seconds. Dividing that by 60 gives you the number of minutes.

How do I calculate time difference between dates in Excel?

Most of the work in this formula is done by the TEXT function, which applies a custom number format for hours and minutes to a value created by subtracting the start date from the end date.

How do I get the difference between two dates and minutes in Python?

timedelta() method. To find the difference between two dates in Python, one can use the timedelta class which is present in the datetime library. The timedelta class stores the difference between two datetime objects.


2 Answers

If your GetItemDate is failing for either date, you will get zero. On the catch statement, place a println or debug to output the issue and see if that tells you something.

Also, just as a matter of practice, I'd change:

return (int)((laterDate.getTime()/60000) - (earlierDate.getTime()/60000));

to:

long result = ((laterDate.getTime()/60000) - (earlierDate.getTime()/60000));
return (int) result;

This gives you the opportunity to view the result of the math in an IDE.

like image 171
Thom Avatar answered Oct 05 '22 07:10

Thom


I debugged through your code: there is ParseException in GetItemDate() for both of the date strings like:

Unparseable date: 11/21/2011 07:00:00 AM

The problem is that parsing AM/PM hours only works with "hh" (small caps!) or "KK". At least that is the case in my phone (it seems some of us didn't see this issue at all). I tested it also works with a single "h" too.

hh = 1-12
KK = 0-11
HH = 0-23
kk = 1-24

Change your SimpleDateFormat line to this:

final SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a", Locale.US);


This post explains the "a" and Locale: Unable to parse DateTime-string with AM/PM marker

PS. I'm from Finland so I must use the "Locale.US" part for this to work. US residents may test the code without it, I think.

like image 28
Jarno Argillander Avatar answered Oct 05 '22 09:10

Jarno Argillander