简体   繁体   English

Java 日历问题:为什么这两个日期不相等?

[英]Java Calendar problem: why are these two Dates not equal?

import java.io.*;

public class testing {
    public static void main(String a[]) throws Exception{
        Date d1=new Date();
        Thread.sleep(2000);
        Date d2=new Date();
        if(d1.equals(d2)){
            System.out.println("Both equal");
        }else{
            System.out.println("Both not equal");
        }
        Calendar c1=Calendar.getInstance();
        Calendar c2=Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        c1.clear(Calendar.HOUR);
        c1.clear(Calendar.MINUTE);
        c1.clear(Calendar.SECOND);
        c2.clear(Calendar.HOUR);
        c2.clear(Calendar.MINUTE);
        c2.clear(Calendar.SECOND);
        if(c2.compareTo(c1) == 0){
            System.out.println("Cal Equal");
        }else {
            System.out.println("Cal Not Equal");
        }
    }
}

When I run the above code multiple times of which each time (for printing if conditions of date) 'Both not equal' is printed but (for if condition of Calendar) sometimes it prints 'Cal Equal' and sometimes 'Cal Not Equal'.当我多次运行上述代码时,每次(用于打印日期条件)“两者不相等”被打印,但(对于日历的条件)有时它会打印“Cal Equal”,有时会打印“Cal Not Equal”。 Can anyone please explain me why this is so?谁能解释一下为什么会这样?

The main reason I was trying this because I want to compare two dates.我尝试这样做的主要原因是因为我想比较两个日期。 Both have same day, month and Year but different time of the day when the objects were created.两者都有相同的日、月和年,但创建对象的时间不同。 I want them to be compared equal(same).我希望它们被比较相等(相同)。 How should I do this?我该怎么做?

Truncate the Milliseconds field截断毫秒字段

Calendars have milliseconds, too.日历也有毫秒。 Add this:添加这个:

c1.clear(Calendar.MILLISECOND);
c2.clear(Calendar.MILLISECOND);

But it's easier to achieve that functionality using DateUtils.truncate() from Apache Commons / Lang但是使用Apache Commons / Lang 的DateUtils.truncate()更容易实现该功能

c1 = DateUtils.truncate(c1, Calendar.DATE);
c2 = DateUtils.truncate(c2, Calendar.DATE);

This removes all hours, minutes, seconds and milliseconds.这将删除所有小时、分钟、秒和毫秒。

I believe Calendar also has Millisecond precision.我相信 Calendar 也有毫秒精度。 If you want to continue your code, add如果你想继续你的代码,添加

c1.clear(Calendar.MILLISECOND);
c2.clear(Calendar.MILLISECOND);

then try your comparison.然后尝试你的比较。

改用JodaTime ,它在操作日期和时间方面要好得多(比标准日期和日历)。

你需要清除毫秒

After之后

Calendar c1=Calendar.getInstance();

add添加

c1.clear();

That's a bit easier to type than c1.clear(Calendar.MILLISECOND);这比c1.clear(Calendar.MILLISECOND);更容易输入c1.clear(Calendar.MILLISECOND);

Works a treat.很好用。

Reset time using the below method before compare dates在比较日期之前使用以下方法重置时间

private Calendar resetHours(Calendar cal) {
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MILLISECOND,0);
        return cal;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM