This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "type casting issue" Watch "type casting issue" New topic
Author

type casting issue

Sridhar Darbha
Greenhorn

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
Greenhorn

Joined: Dec 12, 2006
Posts: 14
Thank you very much Peter.....
I'll try that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: type casting issue
 
Similar Threads
Strange sql problem in jdbc
Issue with Date insertion
Retrieving wrong date/time with Timestamp
convert timestamp to date
Convert String to sql.Date