aspose file tools*
The moose likes Java in General and the fly likes Timestamp.compareTo() problem with jdk1.5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Timestamp.compareTo() problem with jdk1.5 " Watch "Timestamp.compareTo() problem with jdk1.5 " New topic
Author

Timestamp.compareTo() problem with jdk1.5

Kuldeep Tewari
Ranch Hand

Joined: May 22, 2006
Posts: 35
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

Joined: Dec 28, 2006
Posts: 893
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.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timestamp.compareTo() problem with jdk1.5