2

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
5
  • 1
    Crashes with what? Please show stack trace. Commented Mar 6, 2014 at 13:04
  • 1
    Why are you casting the result of parse to a Date when it's a date already? Commented Mar 6, 2014 at 13:05
  • The Thom, I remove the cast and import java.utils.date instead of java.sql.date and it works Commented Mar 6, 2014 at 13:09
  • 1
    Excellent. Please accept one of the answers below, then. Commented Mar 6, 2014 at 13:10
  • @illDev: In future, please make sure that you include relevant context - in this case at least the stack trace - when you ask a question. Commented Mar 6, 2014 at 13:15

4 Answers 4

5

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;
Sign up to request clarification or add additional context in comments.

6 Comments

How do you know that? There's nothing about java.sql.Date in the question, and no indication of the error...
cause...I also faced the same problem.
It's possible that the cast to a Date is an indicator there's a type mismatch
Well you may have faced a problem with somewhat similar symptoms, but I think you're making an assumption that it's the same problem. Now that we have the exception, I suspect you're correct - but I think it would have been wise to be a bit more circumspect than: "you have imported the below package"...
I remove the cast and import java.utils.date instead of java.sql.date and it works
|
1

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

2 Comments

This looks remarkably similar to Hamid's answer, but I don't see any imports in the question at all... where did you get the information that the OP is using java.sql.Date?
Maybye, but at the begening I had the same problems. So it is common mistake. But post might be released in the same time, I didn't see above answer first.
1

Its importing wrong library prob dude just use this

import java.util.Date; 

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.