简体   繁体   English

比较Calendar和java.util.date日期

[英]Compare Calendar and java.util.date dates

Hi i am creating instance of Calendar Using: 嗨,我正在使用以下方法创建Calendar实例:

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). 它打印-1和1而不是0(零)。
Can any one help me to find what's goes wrong ? 谁能帮我找出问题所在?

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)));
    }

}

Date and Calender classes are different. 日期和日历类别不同。 Most of the methods of java.util.Date are deprecated. 不推荐使用java.util.Date的大多数方法。 Hence its better to use Calender class. 因此,最好使用Calender类。 For more info on methods check out the below url. 有关方法的更多信息,请查看以下网址。 http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html

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; 返回:如果参数是一个等于此Date的Date,则返回值0;否则,返回0。 a value less than 0 if the argument is a Date after this Date; 如果参数是此日期之后的日期,则该值小于0; and a value greater than 0 if the argument is a Date before this Date. 如果参数是该日期之前的日期,则该值大于0。

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. 这里会有区别,因为您还需要将毫秒设置为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()将使毫秒保持不变。

where is date1 created? date1在哪里创建的? You seem to assume Calendar and Date are related. 您似乎假设日历和日期相关。 They are not. 他们不是。

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

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