Home » Java » java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date

Posted by: admin December 28, 2021 Leave a comment

Questions:

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

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

###

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

###

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