Question

I have this problem.. I have a Hashmap containing the String as the key and Object as the value. I have one Date Object inside it... While setting in the value for stored procedure call to DB I want to convert it to sql Date type..

Code Snippet..

this.cStmt.setDate(15,(java.sql.Date) (SubsProfile.get(Constants.SUBSC_DETAILS_EXPIRY_DATE_1)));

However while running I am getting ClassCastException..

Exception:-

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

please help me to convert it to appropiate type...

Was it helpful?

Solution

that's because, as the exception says, your object is java.util.Date, and you're casting it to java.sql.Date!

Do this:

java.sql.Date sqlDate = new java.sql.Date(((java.util.Date)SubsProfile.get(Constants.SUBSC_DETAILS_EXPIRY_DATE_1)).getTime());
this.cStmt.setDate(15,sqlDate);

OTHER TIPS

get the long value of the java.util.Date object you have and create a java.sql.Date object using that long value.

java.util.Date utilDate = new java.util.Date();

java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

This should help you convert util.Date to sql.Date

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top