• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Timestamp.compareTo() problem with jdk1.5

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The third line in the following piece of code is throwing ClassCast exception with jdk1.5.

long l=System.currentTimeMillis();
Timestamp ts=new Timestamp(l);
int x=ts.compareTo(new Date());


but the same code runs fine with jdk1.4.
I saw the documentation for both versions. In jdk1.4 compareTo() is defined as:
int compareTo(Object o);

and in jdk1.5 compareTo() is defined as:
int compareTo(Date o);

In both the versions, it is mentioned that the parmeter o for compareTo(), must be a Timestamp object.

Still I'm getting the error only with jdk1.5 setup.

Can anyone help me this?

Thanks in advance.

Kuldeept.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're trying to cast a superclass to a subclass and that will only succeed if that superclass will point the subclass.

In your example this is not the case so a ClassCastException will be thrown.

If you adjust your code like this. Your code will compile



I hope this helps.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code should not work, also not on 1.4, if the API documentation says that the object must be a Timestamp object.

If it does work on Java 1.4, then that's a bug in Java 1.4. If your program relies on this bug, then you should correct your program, because you don't want your program to rely on a bug in a particular Java version.

Have a look at this bug description in Sun's bug database: Bug ID: 4631234 - java.sql.Timestamp.compareTo no longer accepts java.util.Dates in 1.4
[ February 20, 2007: Message edited by: Jesper Young ]
 
A wop bop a lu bop a womp bam boom! Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic