Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

type casting issue

 
Sridhar Darbha
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: -

 
Sridhar Darbha
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Peter.....
I'll try that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic