Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Timestamp.compareTo() problem with jdk1.5

 
Kuldeep Tewari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic