Java 日历问题:为什么这两个日期不相等?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4042817/
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
Java Calendar problem: why are these two Dates not equal?
提问by Shwetanka
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'. Can anyone please explain me why this is so?
当我多次运行上述代码时,每次(用于打印日期条件)“两者不相等”被打印,但(对于日历的条件)有时它会打印“Cal Equal”,有时会打印“Cal Not Equal”。谁能解释一下为什么会这样?
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?
我尝试这样做的主要原因是我想比较两个日期。两者都有相同的日、月和年,但创建对象的时间不同。我希望它们被比较相等(相同)。我该怎么做?
回答by Sean Patrick Floyd
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
但它更容易实现使用功能DateUtils.truncate()从Apache的共享/郎
c1 = DateUtils.truncate(c1, Calendar.DATE);
c2 = DateUtils.truncate(c2, Calendar.DATE);
This removes all hours, minutes, seconds and milliseconds.
这将删除所有小时、分钟、秒和毫秒。
回答by josh.trow
I believe Calendar also has Millisecond precision. If you want to continue your code, add
我相信 Calendar 也有毫秒精度。如果你想继续你的代码,添加
c1.clear(Calendar.MILLISECOND);
c2.clear(Calendar.MILLISECOND);
then try your comparison.
然后尝试你的比较。
回答by NimChimpsky
回答by Maurice Perry
You need to clear milleseconds
你需要清除毫秒
回答by Gary Fry
After
后
Calendar c1=Calendar.getInstance();
add
添加
c1.clear();
That's a bit easier to type than c1.clear(Calendar.MILLISECOND);
这比打字容易一些 c1.clear(Calendar.MILLISECOND);
Works a treat.
很好用。
回答by Shakthifuture
Remove time using below method to 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;
}