Domanda

I`m trying to convert a string to date:

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
È stato utile?

Soluzione

In the below line...

(Date)formatter.parse(str)

You are trying cast java.util.Date type Date object to java.sql.Date type Date object...that's why its throwing this Exception

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

And its causing due to import of the below package...

import java.sql.Date;

You should import the package as below...So, you should remove the above import and import this below one.

import java.util.Date;

Altri suggerimenti

Change libraries :) java.sql.Date to java.util.Date ;) this might help.

Its importing wrong library prob dude just use this

import java.util.Date; 

Try this code

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top