开发者

JAVA:-Converting Object Stored in HashMap to sql Date Type

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(Con开发者_如何学Gostants.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...


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜