Calendar.before()和Calendar.after()返回错误的值

6

我在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())));

这是一个从保存的设置中获取起始日期的想法,然后将6:12:30添加到时钟中,直到它超过当前时间。
然而,我的文本框给出了以下信息:
false: 09/07/12 04:20 - 11/08/12 00:00 false: 09/07/12 04:20 - 11/08/12 00:00
为什么调用“before(11th Aug 2012)”时,2012年7月9日会返回false?
如果我将“.before”更改为“.after”,我会得到这个:
true: 09/07/12 04:20 - 11/08/12 00:00 true: 20/10/12 15:40 - 11/08/12 00:00
这似乎毫无意义。你有什么想法?谢谢。

尝试在比较这两个日期之前记录它们的logCat并发布结果。 - iTurki
3个回答

6
我想知道是否因为您的“年”设置是12而不是2012。那么,您将比较09/07/2012和11/08/0012-但是由于日期格式,您无法确定。
出于诊断目的,请将您的格式字符串更改为“dd/MM/yyyy HH:mm”,然后再试一次。

1

嗯,在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())));

输出:- 真 09/07/12 00:00 - 11/08/12 13:29


-1

我之前遇到过这个问题。但是我不需要比较小时、分钟和秒,只需要比较日期。但是@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;
                }
            }
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接