Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I want to convert string date to date format for storing database table. The code below is not working. It is showing a java.lang.ClassCastException:

java.lang.String cannot be cast to java.util.Date Exception

Code

Date date = (Date)hmap.get("skillexpdate");
share|improve this question

Use SimpleDateFormat#parse() instead; you cannot directly cast a String instance into a Date.

share|improve this answer

You can't simply cast. You need to use SimpleDateFormat.parse() to convert String to Date.

share|improve this answer
    
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date date=formatter.parse(hmap.get("skillexpdate").toString()); i changed the code above format still it is showing same exception.. – Ajithlal Nov 8 '13 at 21:52
    
@Ajithlal: Parameter format changes based on what format your String is. For example if String is in format 2013-09-27, then your code in above comment will work fine. – Nambari Nov 8 '13 at 21:53

You can use this code :

DateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
java.sql.Date dutyDay = (java.sql.Date) simpleDateFormat.parse("There is String Date");
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.