File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes type casting issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "type casting issue" Watch "type casting issue" New topic

type casting issue

Sridhar Darbha

Joined: Dec 12, 2006
Posts: 14
I have a java file which imports java.sql.Date class.
How can I typecast an object of type java.sql.Timestamp to java.sql.Date , in that file ?
Both java.sql.Timestamp and java.sql.Date extend from java.util.Date class.

Thanks in advance.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
You can't. A typecast of an object reference cannot change the object into a different kind of object.

If you had an object reference that was declared as a java.util.Date, but you knew that it actually referred to one of the subclasses, java.sql.Date or java.sql.Timestamp, you could typecast the object reference to one of those subclasses.

If you have a java.sql.Timestamp and you want to make a java.sql.Date, you need to construct a new java.sql.Timestamp that represents the same time as the java.sql.Date does. You can do that with: -

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Sridhar Darbha

Joined: Dec 12, 2006
Posts: 14
Thank you very much Peter.....
I'll try that.
I agree. Here's the link:
subject: type casting issue
It's not a secret anymore!