java.lang.ClassCastException: java.lang.String 不能转换为 java.util.Date

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19868930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:53:35  来源:igfitidea点击:

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

javamysql

提问by Ajithlal

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.ClassCastException

java.lang.String cannot be cast to java.util.Date Exception

Code

代码

Date date = (Date)hmap.get("skillexpdate");

回答by kosa

You can't simply cast. You need to use SimpleDateFormat.parse()to convert Stringto Date.

你不能简单地投射。您需要使用SimpleDateFormat.parse()转换StringDate.

回答by Vivin Paliath

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

使用SimpleDateFormat#parse()代替; 您不能直接将String实例转换为Date.

回答by Suny

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