Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

mysql

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");
like image 804
Ajithlal Avatar asked Nov 08 '13 21:11

Ajithlal


People also ask

How do I resolve Java Lang ClassCastException?

// type cast an parent type to its child type. In order to deal with ClassCastException be careful that when you're trying to typecast an object of a class into another class ensure that the new type belongs to one of its parent classes or do not try to typecast a parent object to its child type.

What is ClassCastException in Java?

ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.

What is Java SQL date?

A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT. To conform with the definition of SQL DATE , the millisecond values wrapped by a java.


2 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");
like image 154
Suny Avatar answered Oct 25 '22 13:10

Suny


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

like image 45
Vivin Paliath Avatar answered Oct 25 '22 13:10

Vivin Paliath