java.lang.ClassCastException

Discussion in 'Java' started by nash_rack1@yahoo.com, May 1, 2006.

  1. Guest

    This piece of code works ok in JDK 1.4 and throws ClassCastException in
    1.5 on the line with compareTo call:

    java.sql.Date d1 = new java.sql.Date(new java.util.Date().getTime());
    java.sql.Timestamp d2 = new java.sql.Timestamp(new
    java.util.Date().getTime());

    java.util.Date ud1 = d1;
    java.util.Date ud2 = d2;

    int diff = ud2.compareTo(ud1);

    Is this correct behavior in 1.5 or a bug? How can I make it use the
    compareTo method in java.util.Date?

    Nash.
    , May 1, 2006
    #1
    1. Advertising

  2. Bjorn Abelli Guest

    <> wrote...

    > This piece of code works ok in JDK 1.4 and
    > throws ClassCastException in
    > 1.5 on the line with compareTo call:
    >
    > java.sql.Date d1 = new java.sql.Date(new java.util.Date().getTime());
    > java.sql.Timestamp d2 = new java.sql.Timestamp(new
    > java.util.Date().getTime());
    >
    > java.util.Date ud1 = d1;
    > java.util.Date ud2 = d2;
    >
    > int diff = ud2.compareTo(ud1);
    >
    > 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




    Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
    Bjorn Abelli, May 1, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Dubery
    Replies:
    5
    Views:
    584
    Bob Dubery
    Jun 19, 2004
  2. mgalaty
    Replies:
    0
    Views:
    2,720
    mgalaty
    Sep 10, 2004
  3. patrick
    Replies:
    0
    Views:
    3,417
    patrick
    Aug 20, 2005
  4. Paolo

    java.lang.ClassCastException

    Paolo, Apr 1, 2006, in forum: Java
    Replies:
    10
    Views:
    4,975
    Mike Schilling
    Apr 2, 2006
  5. marco
    Replies:
    6
    Views:
    22,109
    qcarver
    May 18, 2007
Loading...

Share This Page