gpt4 book ai didi

java - 错误将字符串转换为日期 : ClassCastException

转载 作者:太空狗 更新时间:2023-10-29 15:53:26 24 4
gpt4 key购买 nike

我正在尝试将字符串转换为日期:

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

最佳答案

在下一行...

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

关于java - 错误将字符串转换为日期 : ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22225386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com