两个日期之间的差异,以分钟为单位

时间:2021-10-09 06:10:19

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.

我需要得到两个日期之间的分钟数。我知道Joda是最好用的,但是这是针对Android项目的,所以我更喜欢尽可能使用一些外部库,而我正在构建的应用程序并不需要通过手术精确计算。

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:

但是,我使用的代码似乎不起作用。我想要获得“11/21/2011 7:00:00 AM”和“11/21/2011 1:00:00 PM”之间的分钟数(应该是360分钟),但是代码我使用返回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));
}

2 个解决方案

#1


5  

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

我通过你的代码进行了调试:GetItemDate()中有两个日期字符串的ParseException:

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

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.

问题是解析AM / PM小时仅适用于“hh”(小型大写!)或“KK”。至少在我的手机中就是这种情况(似乎我们中的一些人根本没有看到这个问题)。我测试它也适用于单个“h”。

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

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

Change your SimpleDateFormat line to this:

将SimpleDateFormat行更改为:

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

这篇文章解释了“a”和Locale:无法使用AM / PM标记解析DateTime-string

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.

PS。我来自芬兰所以我必须使用“Locale.US”部分来实现这个目的。我想,美国居民可能会在没有它的情况下测试代码。

#2


13  

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.

如果您的GetItemDate失败了任何一个日期,您将得到零。在catch语句中,放置println或debug来输出问题,看看是否能告诉你什么。

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.

这使您有机会在IDE中查看数学结果。

#1


5  

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

我通过你的代码进行了调试:GetItemDate()中有两个日期字符串的ParseException:

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

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.

问题是解析AM / PM小时仅适用于“hh”(小型大写!)或“KK”。至少在我的手机中就是这种情况(似乎我们中的一些人根本没有看到这个问题)。我测试它也适用于单个“h”。

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

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

Change your SimpleDateFormat line to this:

将SimpleDateFormat行更改为:

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

这篇文章解释了“a”和Locale:无法使用AM / PM标记解析DateTime-string

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.

PS。我来自芬兰所以我必须使用“Locale.US”部分来实现这个目的。我想,美国居民可能会在没有它的情况下测试代码。

#2


13  

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.

如果您的GetItemDate失败了任何一个日期,您将得到零。在catch语句中,放置println或debug来输出问题,看看是否能告诉你什么。

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.

这使您有机会在IDE中查看数学结果。