我正在尝试将字符串转换为日期:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Date dataEvento = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
dataEvento = (Date)dateFormat.parse("28/08/2010"); //the app crash here
logcat的:
03-06 10:06:19.867: E/AndroidRuntime(426): FATAL EXCEPTION: main
03-06 10:06:19.867: E/AndroidRuntime(426): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ladessa.jccb/com.ladessa.jccb.MainActivity}: java.lang.ClassCastException: java.util.Date
03-06 10:06:19.867: E/AndroidRuntime(426): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-06 10:06:19.867: E/AndroidRuntime(426): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-06 10:06:19.867: E/AndroidRuntime(426): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-06 10:06:19.867: E/AndroidRuntime(426): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-06 10:06:19.867: E/AndroidRuntime(426): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 10:06:19.867: E/AndroidRuntime(426): at android.os.Looper.loop(Looper.java:123) 03-06 10:06:19.867: E/AndroidRuntime(426): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-06 10:06:19.867: E/AndroidRuntime(426): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 10:06:19.867: E/AndroidRuntime(426): at java.lang.reflect.Method.invoke(Method.java:521)
03-06 10:06:19.867: E/AndroidRuntime(426): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-06 10:06:19.867: E/AndroidRuntime(426): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-06 10:06:19.867: E/AndroidRuntime(426): at dalvik.system.NativeStart.main(Native Method) 03-06 10:06:19.867: E/AndroidRuntime(426): Caused by: java.lang.ClassCastException: java.util.Date
03-06 10:06:19.867: E/AndroidRuntime(426): at com.ladessa.jccb.MainActivity.createScheduledNotification(MainActivity.java:59)
03-06 10:06:19.867: E/AndroidRuntime(426): at com.ladessa.jccb.MainActivity.onCreate(MainActivity.java:110)
03-06 10:06:19.867: E/AndroidRuntime(426): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-06 10:06:19.867: E/AndroidRuntime(426): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-06 10:06:19.867: E/AndroidRuntime(426): ... 11 more
答案 0 :(得分:5)
在下面一行......
(Date)formatter.parse(str)
您正在尝试将java.util.Date
类型Date
对象投放到java.sql.Date
类型Date
对象...这就是为什么它会抛出此Exception
java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
由于导入以下包裹而引起的......
import java.sql.Date;
您应该按如下方式导入包...因此,您应该删除上面的导入并将其导入下面。
import java.util.Date;
答案 1 :(得分:1)
更改库:) java.sql.Date to java.util.Date
;)这可能会有所帮助。
答案 2 :(得分:1)
导入错误的库prob dude只是使用这个
import java.util.Date;
答案 3 :(得分:1)
试试此代码
String Stringdate = "28/08/2010";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(Stringdate);
} catch (ParseException e) {
// TODO Auto-generated catch block
System.err.println("Date Conversion Error : " + e.getMessage());
}
System.out.println(convertedDate);