java Calendar.before() 和 Calendar.after() 返回不正确的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11916743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calendar.before() and Calendar.after() returning incorrect values
提问by Marq Watkin
I'm getting a bit stuck with date comparison in Java/Android
我对 Java/Android 中的日期比较有些困惑
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm");
Calendar now = Calendar.getInstance();
Calendar c;
c = Calendar.getInstance();
c.set(settings.getInt("year", 0), settings.getInt("month", 0), settings.getInt("day", 0), settings.getInt("hour", 0), settings.getInt("minute", 0));
views.setTextViewText(R.id.textView4, String.valueOf(c.getTime().before(now.getTime())));
views.setTextViewText(R.id.textView2, String.format("%s - %s", df.format(c.getTime()), df.format(now.getTime())));
int timeout = 0;
while( c.getTime().before(now.getTime()) )
{
c.add(Calendar.SECOND, 30);
c.add(Calendar.MINUTE, 12);
c.add(Calendar.HOUR_OF_DAY, 6);
isHigh = 1 - isHigh;
timeout++;
if( timeout >= 400 )
break;
}
views.setTextViewText(R.id.textView5, String.valueOf(c.getTime().before(now.getTime())));
views.setTextViewText(R.id.textView3, String.format("%s - %s", df.format(c.getTime()), df.format(now.getTime())));
The idea of this is it takes a starting date from saved settings, and adds 6:12:30 to the clock until it passes the current time.
这样做的想法是从保存的设置中获取开始日期,并将 6:12:30 添加到时钟,直到它通过当前时间。
However, my textboxes give me the following:
但是,我的文本框给了我以下内容:
false : 09/07/12 04:20 - 11/08/12 00:00
false : 09/07/12 04:20 - 11/08/12 00:00
假:09/07/12 04:20 - 11/08/12 00:00
假:09/07/12 04:20 - 11/08/12 00:00
Why is 9th July 2012 returning false when calling "before(11th Aug 2012)"?
为什么 2012 年 7 月 9 日在调用“before(2012 年 8 月 11 日)”时返回 false?
If I change the ".before" to ".after", I get this:
如果我将“.before”更改为“.after”,我会得到:
true : 09/07/12 04:20 - 11/08/12 00:00
true : 20/10/12 15:40 - 11/08/12 00:00
真:09/07/12 04:20 - 11/08/12 00:00
真:20/10/12 15:40 - 11/08/12 00:00
Which doesn't seem to make any sense.
这似乎没有任何意义。
Any ideas what I'm doing wrong? Thanks
任何想法我做错了什么?谢谢
回答by Jon Skeet
I wonder whether it's because your "year" setting is 12 rather than 2012. Then you'd be comparing 09/07/2012 with 11/08/0012 - but you can't tell because of your date format.
我想知道是不是因为您的“年份”设置是 12 而不是 2012 年。然后您将 09/07/2012 与 11/08/0012 进行比较 - 但由于您的日期格式,您无法分辨。
For diagnostic purposes, change your format string to "dd/MM/yyyy HH:mm"
and try again.
出于诊断目的,请将格式字符串更改为 并重"dd/MM/yyyy HH:mm"
试。
回答by vivek kumar
Well, its working as expected in JDK with 12 as well as 2012.
好吧,它在 JDK 12 和 2012 中按预期工作。
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm");
Calendar now = Calendar.getInstance();
Calendar c;
c = Calendar.getInstance();
c.set(12, 6, 9, 0, 0);
System.out.println(String.valueOf(c.getTime().before(now.getTime())));
System.out.println(String.format("%s - %s", df.format(c.getTime()), df.format(now.getTime())));
Output:- true 09/07/12 00:00 - 11/08/12 13:29
输出:- true 09/07/12 00:00 - 11/08/12 13:29
回答by valerybodak
I had this issue earlier. But I was not required to compare hours, minutes and seconds. Only date. But @Jon Skeet's answer not worked for me. So I've implemented my own method for compare two dates. Ugly, but works fine.
我之前有这个问题。但我不需要比较小时、分钟和秒。只有日期。但是@Jon Skeet 的回答对我不起作用。所以我已经实现了我自己的方法来比较两个日期。丑陋,但工作正常。
private boolean before(Calendar c1, Calendar c2){
int c1Year = c1.get(Calendar.YEAR);
int c1Month = c1.get(Calendar.MONTH);
int c1Day = c1.get(Calendar.DAY_OF_MONTH);
int c2Year = c2.get(Calendar.YEAR);
int c2Month = c2.get(Calendar.MONTH);
int c2Day = c2.get(Calendar.DAY_OF_MONTH);
if(c1Year<c2Year){
return true;
}else if (c1Year>c2Year){
return false;
}else{
if(c1Month>c2Month){
return false;
}else if(c1Month<c2Month){
return true;
}else{
if(c1Day<c2Day){
return true;
}else{
return false;
}
}
}
}