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");
// 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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With