<nash_rack1@yahoo.com> wrote...
> This piece of code works ok in JDK 1.4 and
> throws ClassCastException in
[quoted text clipped - 10 lines]
>
> Is this correct behavior in 1.5 or a bug?
According to the API, that's the correct behavior since 1.5:
-------------------------------------
public int compareTo(Date o)
Compares this Timestamp object to the given Date, which must be a Timestamp
object. If the argument is not a Timestamp object, this method throws a
ClassCastException object. (Timestamp objects are comparable only to other
Timestamp objects.)
-------------------------------------
The explanation for this can be read at the introduction to the Timestamp
class:
/.../
Due to the differences between the Timestamp class and the java.util.Date
class mentioned above, it is recommended that code not view Timestamp values
generically as an instance of java.util.Date. The inheritance relationship
between Timestamp and java.util.Date really denotes implementation
inheritance, and not type inheritance.
/.../
However, when I tried it in 1.6 it *didn't* complain as it should...
> How can I make it use the
> compareTo method in java.util.Date?
It depends, but maybe something like this:
java.util.Date ud1 =
new java.util.Date( d1.getTime() );
java.util.Date ud2 =
new java.util.Date( d2.getTime() );
// Bjorn A