开发者

SimpleDateFormat gives java.lang.classcastexception: java.util.date

开发者 https://www.devze.com 2023-03-13 01:16 出处:网络
String str = \"13/06/2011\"; SimpleDateFormat formatter =开发者_如何学C new SimpleDateFormat(\"dd/MM/yyyy\");
String str = "13/06/2011";
SimpleDateFormat formatter =开发者_如何学C new SimpleDateFormat("dd/MM/yyyy");
Date date = (Date)formatter.parse(str);


I guess that your Date class is actually a java.sql.Date.


What does your import statement say? Are you importing some other class (for example java.sql.Date) by accident? What does the compiler say when you remove the class cast (which should not be there)?


DateFormat.parse() returns an instance of java.util.Date and not java.sql.Date. In order to convert from java.util.Date to java.sql.Date, I do the following:

java.util.Date fromDate = df.parse(fromdate1);  
java.sql.Date sqlDate = new java.sql.Date(fromDate.getTime()); 
0

精彩评论

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