差,在几分钟内,两个日期之间 [英] Difference, in minutes, between two dates

查看:136
本文介绍了差,在几分钟内,两个日期之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到两个日期之间的分钟数。我知道乔达是最好的使用,但这是一个Android的项目,所以我preFER使用小的外部库成为可能,以及应用程序,我的建筑不需要计算是手术precise。

不过,code我使用似乎并不奏效。我试图获得2011年11月21日上午07时00分00秒和2011年11月21日下午一点00分00秒(应为360分)之间的分钟数,而是code我使用返回0:

  INT差异= minutesDiff(GetItemDate(2011年11月21日早上7:00:00点),GetItemDate(2011年11月21日下午1点00分00秒) );

公共静态日期GetItemDate(最后弦乐日)
{
    最终日历CAL = Calendar.getInstance(TimeZone.getDefault());
    最后SimpleDateFormat的格式=新的SimpleDateFormat(MM / DD / YYYY HH:MM:SS);
    format.setCalendar(卡);

    尝试 {
        返回format.parse(日期);
    }赶上(ParseException的E){
        返回null;
    }
}

公共静态INT minutesDiff(日期earlierDate,日期laterDate)
{
    如果(earlierDate == NULL || laterDate == NULL)返回0;

    返程(INT)((laterDate.getTime()/ 60000) - (earlierDate.getTime()/ 60000));
}
 

解决方案

我调试通过你的code:有 ParseException的 GetItemDate ()这两个日期字符串,如:

  

不可解析日期:2011年11月21日上午07时零零分00秒

问题是,解析AM / PM小时,只适用于HH(小盘股!)或KK。至少这是我的电话的情况下(看来我们有些人没有看到这个问题的话)。我测试了它也可以用一个H了。

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

你的的SimpleDateFormat 行改成这样:

最后SimpleDateFormat的格式=新的SimpleDateFormat(MM / DD / YYYY HH:MM:SS,Locale.US) ;


这篇文章解释了a和区域设置:<一href="http://stackoverflow.com/questions/3618676/unable-to-parse-datetime-string-with-am-pm-marker">Unable解析日期时间字符串带AM / PM标记

PS。我来自芬兰我,所以我必须使用Locale.US部分这个工作。美国居民可测试code没有它,我想。

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));
}

解决方案

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.

这篇关于差,在几分钟内,两个日期之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆