比较 Calendar 和 java.util.date 日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11542039/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:30:17  来源:igfitidea点击:

Compare Calendar and java.util.date dates

javadate

提问by Vipul

Hi i am creating instance of Calendar Using:

嗨,我正在使用以下方法创建日历实例:

Calendar calendarDate=Calendar.getInstance();
            calendarDate.set(2012, 6, 1,0,0,0);
                Date date1=calendar.getTime();

After that i create instance of java.util.date using:

之后,我使用以下方法创建 java.util.date 的实例:

Date date2=new Date(2012 - 1900,7-1, 01);

Now when i am trying to compare dates using following code:

现在,当我尝试使用以下代码比较日期时:

System.out.println(date2.compareTo(date1));
System.out.println(date1.compareTo(date2));

it prints -1 and 1 instead of 0(zero).
Can any one help me to find what's goes wrong ?

它打印 -1 和 1 而不是 0(零)。
任何人都可以帮我找出问题所在吗?

回答by Francisco Spaeth

Try to clear the value of calendar before set a new one. This will solve your problem.

尝试在设置新日历之前清除日历的值。这将解决您的问题。

Calendar calendarDate=Calendar.getInstance();
calendarDate.clear();
calendarDate.set(2012, 6, 1,0,0,0);

Test for it:

测试一下:

import java.util.Calendar;
import java.util.Date;

import org.junit.Assert;

public class DateTest {

    @org.junit.Test
    public void test() {
        Calendar calendarDate=Calendar.getInstance();
        calendarDate.clear();
        calendarDate.set(2012, 6, 1,0,0,0);
        Assert.assertEquals(0, calendarDate.getTime().compareTo(new Date(2012 - 1900,7-1, 01)));
    }

}

回答by Rajath

Date and Calender classes are different. Most of the methods of java.util.Date are deprecated. Hence its better to use Calender class. For more info on methods check out the below url. http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html

Date 和 Calender 类是不同的。java.util.Date 的大多数方法都已弃用。因此最好使用 Calender 类。有关方法的更多信息,请查看以下网址。 http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html

回答by nate_weldon

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Date.html#compareTo(java.lang.Object)

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Date.html#compareTo(java.lang.Object)

compareTo

相比于

Returns: the value 0 if the argument is a Date equal to this Date; a value less than 0 if the argument is a Date after this Date; and a value greater than 0 if the argument is a Date before this Date.

返回: 如果参数是等于此日期的日期,则值为 0;如果参数是此日期之后的日期,则为小于 0 的值;如果参数是此日期之前的日期,则该值大于 0。

回答by srini.venigalla

where is date1 created? You seem to assume Calendar and Date are related. They are not.

date1 在哪里创建?您似乎认为 Calendar 和 Date 是相关的。他们不是。

回答by Peter Lawrey

Dates are GMT except when using the deprecated constructors. There will be a difference here because you also need to set the milli-seconds to 0 as well.

日期是 GMT,除非使用已弃用的构造函数。这里会有所不同,因为您还需要将毫秒设置为 0。

Calendar calendarDate=Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendarDate.set(2012, Calendar.JULY, 1,0,0,0);
calendarDate.set(Calendar.MILLISECOND, 0);
Date date1 = calendarDate.getTime();
Date date2=new Date(2012 - 1900,7-1, 1, 1, 0, 0);
System.out.println(date1);
System.out.println(date2);
System.out.println(date2.compareTo(date1));
System.out.println(date1.compareTo(date2));

prints

印刷

Sun Jul 01 01:00:00 BST 2012
Sun Jul 01 01:00:00 BST 2012
0
0

Note: Calendar will leave any field you don't set so the first set() will leave the milliseconds untouched.

注意:日历将保留您未设置的任何字段,因此第一个 set() 将保持毫秒不变。