转换日期格式时获取异常

时间:2014-02-08 10:20:16

标签: android date date-format

在我的应用程序中,我使用了自定义日历视图和日历适配器,我通过Intent以CalendarView.java格式从自定义yyyy-MM-dd获取日期,并且其工作正常。但是,当我尝试以dd-MM-yyyy格式转换它时,该应用程序正在崩溃。

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

这是我的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)

我哪里错了?

请帮忙。

感谢。

3 个答案:

答案 0 :(得分:1)

跟踪显示错误:

Unknown pattern character 'Y'

要修改它:

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

要:

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

答案 1 :(得分:1)

一定是

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

而不是

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

“Y”是一个未知的模式字符。所以不要使用它,否则你会 获取 IllegalArgumentException

答案 2 :(得分:0)

在java日期格式中没有年份的资本i所以请使用小y

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

替换为

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