0

In my App,I have used Custom Calender View and Calender Adapter and I am getting date from Custom CalendarView.java in yyyy-MM-dd format through Intent and Its all working fine. But when I am trying to convert it in dd-MM-yyyy format, the app is getting crashed.

// Call Back method to get the Message form other Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // check if the request code is same as what is passed here it is
    // ACTIVITY_REQUESTCODE_CALANDER
    if (requestCode == 1) {
        if (null != data) {
            // fetch the message String
            String selectedDate = data.getStringExtra("SELECTEDDATE");

            DateFormat formatter = new SimpleDateFormat("YYYY-MM-DD");
            try {
                Date date = (Date) formatter.parse(selectedDate);
                SimpleDateFormat newFormat = new SimpleDateFormat(
                        "MM-dd-yyyy");
                // Or SimpleDateFormat newFormat = new
                // SimpleDateFormat("dd-MM-yyyy");
                String finalString = newFormat.format(date);
                Toast.makeText(MainActivity.this, finalString,
                        Toast.LENGTH_LONG).show();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // Set the message string in textView
            editTextSelectDate.setText(selectedDate);
        }
    }
}

This is my LogCat,

02-08 15:36:12.249: E/AndroidRuntime(4848): FATAL EXCEPTION: main
02-08 15:36:12.249: E/AndroidRuntime(4848): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=1, data=Intent { (has extras) }} to activity {com.example.calendardemo/com.example.calendardemo.MainActivity}: java.lang.IllegalArgumentException: Unknown pattern character 'Y'
02-08 15:36:12.249: E/AndroidRuntime(4848):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3209)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3252)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at android.app.ActivityThread.access$1200(ActivityThread.java:143)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at android.os.Looper.loop(Looper.java:137)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at android.app.ActivityThread.main(ActivityThread.java:4963)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at java.lang.reflect.Method.invokeNative(Native Method)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at java.lang.reflect.Method.invoke(Method.java:511)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at dalvik.system.NativeStart.main(Native Method)
02-08 15:36:12.249: E/AndroidRuntime(4848): Caused by: java.lang.IllegalArgumentException: Unknown pattern character 'Y'
02-08 15:36:12.249: E/AndroidRuntime(4848):     at java.text.SimpleDateFormat.validateFormat(SimpleDateFormat.java:268)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at java.text.SimpleDateFormat.validatePattern(SimpleDateFormat.java:316)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:369)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:253)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at com.example.calendardemo.MainActivity.onActivityResult(MainActivity.java:52)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at android.app.Activity.dispatchActivityResult(Activity.java:5368)
02-08 15:36:12.249: E/AndroidRuntime(4848):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)

Where am I wrong ?

Please help.

Thanks.

0

3 Answers 3

1

It must be

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd);

instead of

DateFormat formatter = new SimpleDateFormat("YYYY-MM-DD");

in SimpleDateFormat "Y" is a Unknown pattern character. So don't use it and otherwise you will get IllegalArgumentException.

Sign up to request clarification or add additional context in comments.

1 Comment

And notice a second change, that "DD" was replaced by "dd" in this correct answer. Uppercase means "day-in-year" rather than "day-in-month".
1

The trace says what's wrong:

Unknown pattern character 'Y'

To fix it change:

DateFormat formatter = new SimpleDateFormat("YYYY-MM-DD");

To:

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd);

Comments

0

there does not have captial y for year in java date format so use small y

DateFormat formatter = new SimpleDateFormat("YYYY-MM-DD");

replace by

DateFormat formatter = new SimpleDateFormat("yyy-MM-DD");

1 Comment

Whoops, you missed the problem with "DD" also. See the correct answer.

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.