Mon, 27 Oct 2008 16:42:21 -0400, /Harold Yarmouth/:
> Calendar calendar = Calendar.getInstance();
> calendar.set(2003,9,10,13,8,42);
> Date date = calendar.getTime();
>
> When the above code is run twice with some time passing in between, the
> resulting Date objects compare unequal!
I think it is o.k. as you're not setting the field for the
milliseconds which is left at what it got set during the time of the
calendar construction. The documentation for the Calendar.set(int,
int, int, int, int, int) method
<http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#set%28int,%20int,%20int,%20int,%20in t,%20int%29>
says:
> Sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR,
> MINUTE, and SECOND. Previous values of other fields are retained.
> If this is not desired, call clear() first.
So you may either call clear() or set(MILLISECOND, 0):
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(2003,9,10,13,8,42);
Date date = calendar.getTime();
System.out.println(date.getTime());
calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(2003,9,10,13,8,42);
Date date2 = calendar.getTime();
System.out.println(date2.getTime());
System.out.println(date.equals(date2));
--
Stanimir
|