1

我有以下设置,需要将其日期实例与当前日期进行比较。虽然两个日期相同,但比较返回 false !

MyClass.java

import java.util.Date;
public class MyClass {
   private Date date;

   ...

}

我的代码

 ....
 Set <MyClass> myclass = new HashSet();

 I populate it with some data here...

 for(MyClass m : myclass)
 {
   System.err.println("date>>:" + trim(m.getDate()));  //returns 2013-08-08
   System.err.println("date>>:" + trim(getCurrentDate()));  //returns 2013-08-08
   System.err.println("boolean:" +                            
               trim(m.getDate()).equals(trim(getCurrentDate()))); //returns false
 }
}

 public Date getCurrentDate() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();
    dateFormat.format(date));
    return date;
}

public Date trim(Date date){
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.HOUR, 0);
    return calendar.getTime();
}
4

5 回答 5

1

日期不一样,它们可能相差毫秒/秒。Date equals不依赖于日期格式,而是比较值。下面的代码也会返回 false:

Date d1 = new Date();
        SimpleDateFormat f = new SimpleDateFormat("yyyy-mm-dd");
        Date d2 = new Date();
        f.format(d2);
        System.out.println(d1);//e.g. Thu Aug 08 12:09:24 IST 2013
        System.out.println(d2);//e.g. Thu Aug 08 12:09:26 IST 2013
        System.out.println(d1.equals(d2));//false

Date.equals比较时间 ( Date.getTime()),仅当它们匹配时equals才会返回:true

public boolean equals(Object obj) {
        return obj instanceof Date && getTime() == ((Date) obj).getTime();
    }

根据 javadoc:

 The result is true if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object. 

Thus, two Date objects are equal if and only if the getTime method returns the same long value for both. 

Date.getTime返回自 1970 年 1 月 1 日 00:00:00 GMT 以来的毫秒数

因此,在您更新的问题中trim,考虑您正在比较两个long时间值(以毫秒为单位)。

如果您需要比较yyyy-MM-dd两个不同date实例的值,请考虑String.equals改用(hack 方式):

SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = f.format(new Date());//2013-08-08
        String date2 = f.format(new Date());//2013-08-08
        System.out.println(date1.equals(date2));
于 2013-08-08T06:41:46.587 回答
1

Date 类包括精确到毫秒的时间,以及比较相等时的时间计数。

要仅比较“日期”部分,您可以执行以下几项操作之一,例如将日期格式化为年-月-日并比较结果字符串,或者从日期创建日历对象并分别比较年、月和日。另一种选择是确保您比较的日期在一天中具有相同的时间,例如 12:00,这样您就可以使用 equals 方法。

于 2013-08-08T06:38:38.173 回答
1

每次你打电话getCurrentDate,你可能会收到一个新的约会。以您的方式对其进行格式化基本上是无操作的,并且日期仍然带有小时、分钟、秒和毫秒。

因此,它们实际上可能实际上是不同的。

您可以删除这些额外信息以获得所需的行为。

于 2013-08-08T06:39:27.470 回答
1

您可以使用GregorianCalendarandCalendar#get(..)来仅比较年、月和日。javadoc
中有一个完美的示例:

 // get the supported ids for GMT-08:00 (Pacific Standard Time)
 String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
 // if no ids were returned, something is wrong. get out.
 if (ids.length == 0)
     System.exit(0);

  // begin output
 System.out.println("Current Time");

 // create a Pacific Standard Time time zone
 SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);

 // set up rules for Daylight Saving Time
 pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
 pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);

 // create a GregorianCalendar with the Pacific Daylight time zone
 // and the current date and time
 Calendar calendar = new GregorianCalendar(pdt);
 Date trialTime = new Date();
 calendar.setTime(trialTime);

 // print out a bunch of interesting things
 System.out.println("ERA: " + calendar.get(Calendar.ERA));
 System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
 System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
 System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
 System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
 System.out.println("DATE: " + calendar.get(Calendar.DATE));
 System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
 System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
 System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
 System.out.println("DAY_OF_WEEK_IN_MONTH: "
                    + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
 System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
 System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
 System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
 System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
 System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
 System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
 System.out.println("ZONE_OFFSET: "
                    + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
 System.out.println("DST_OFFSET: "
                    + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
于 2013-08-08T06:46:25.927 回答
1

Actually there is a problem in your method..

 public Date getCurrentDate() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();
    dateFormat.format(date);
    return date;
}

dateFormat.format(date) will return a String date in yyyy-MM-dd format but you are returning date from this method which will return the Date in 'Thu Aug 08 12:21:34 IST 2013' this format not in '2013-08-08' this. So you should take the String as return from this method and then compare it by equals. Try this, I think this should help you.

于 2013-08-08T06:58:42.880 回答