English 中文(简体)
两个日期之间的时间差异
原标题:Difference, in minutes, between two dates

我需要在两个日期之间获得会议记录。 我知道Joda是最佳用途,但这是一个安乐团项目,因此,我更希望尽可能使用一些外部图书馆,而Im大楼的计算方法并不要求精确。

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));
}
最佳回答

我通过你的代码打字:在<代码>GetItemDate(上,有<代码>ParseException。

Unparabledate: 11/21/2011 07:00:AM00

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

将<代码>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

PS。 因此,我必须使用“当地人”。 美国”参与这项工作。 我认为,美国居民可以在没有标准的情况下测试该守则。

问题回答

如果你任满一天,你将获得零。 在渔获量说明中,用纸质或字眼来研究这一问题,看看这是否告诉你。

同样,作为实践问题,我也作了改动:

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

:

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

这使你有机会在民主选举学会中看到数学的结果。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签