6

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");

3 Answers 3

7

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");
5

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

1

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

2
  • 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.. Commented Nov 8, 2013 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. Commented Nov 8, 2013 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.