This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Casting a String object into a Date ... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Casting a String object into a Date ..." Watch "Casting a String object into a Date ..." New topic
Author

Casting a String object into a Date ...

Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Hi,

I have this strange case!

I have a String object which i want to cast into a Date object.

I'm doing the following:



But this is throwing a a ClassCastException.

Can you please tell me how to do that ?

Note: I know i can use APIs, but what i'm trying to do is solving it in a very generic way, because, at runtime i don't know what types I'm casting to, the values of the class types are stored in a map which the runtime picks from and casts to.

Thanks,


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
AFAIK, Date doesn't have a cast(...) method.
Could you post an SSCCE * that demonstrates this behaviour?

* http://sscce.org
[ August 17, 2008: Message edited by: Piet Verdriet ]
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3189
if you need a generic way of doing it look at the visitor deign pattern or make use of the instanceof (over use can adversely affect performance) construct.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
What you are trying to do is not a cast. What you are trying to do is a conversion. And there is not generic way to do that.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

A couple of ideas...

Take a look at the DateFormat class and it's parse() methods. The parse methods can be used to parse a String into a Date object. Also be sure to look at the setLenient(boolean) method as well.

To do this, you'll need to know you have a String object. So you can either do an "instanceof" check to see if it is a String, or just call the toString() method on whatever object you have. That will, obviously, return a String representation of the object. If that representation can be parsed, you have your Date object. If not, a ParseException is thrown. That's probably the closest you'll come to a generic way of doing something like this.

If your map can possibly contain a Date object, I would at least use instanceof so you simply use that object rather than go through all the work of converting a Date to a String only to be converted back to a Date.

Another option... if all (or most) of the objects that are going to be put in the map are your own objects, create some interface, such as DateConvertible, with a single toDate() method (that returns a Date). You can then write a custom method for each object type. Use an instanceof check to see if the object is an instance of DateConvertible. If so, cast it to a DateConvertible and then call the toDate() method. If not, use the toString methodology from above. That will hopefully cover most cases for you.

Another idea is to use a type of Strategy pattern. If your Map is going to be limited to a fixed number of pre-known object types, you can create a convertorfor each type that implements some interface, say "DateConverter". Use a naming convention of "<ClassName>DateConverter" for each converter. Then using the getSimpleClassName, you can create the name of the class you need. Lastly, use one of the forName() methods and the newInstance() method to get an instance of the necessary Converter:



This last idea will of course make your code some what brittle.

Just some ideas to get you started.
[ August 17, 2008: Message edited by: Mark Vedder ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Casting a String object into a Date ...
 
Similar Threads
polymorphism
Dynamic Casting
how can i use Class.cast() method?
Casting to Vs toString()
Objects (please can someone clear this up)