• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

type casting issue

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Peter.....
I'll try that.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic