java.text.ParseException:Unparseable date:" 2016年10月16日" (抵消3)

时间:2016-10-16 11:35:57

标签: java android

我尝试转换日期格式来自" 2016年10月16日"至" 2016-10-16 00:00:00"反之亦然。第一种格式用于在应用程序上显示,第二种格式用于发送到sql数据库。我使用以下代码,

convertedBirthdate = Helper.convertDateFormat(birthdate,
                    "d mmmm yyyy", "yyyy-mm-dd hh:mm:ss");
public static String convertDateFormat(String dateTime, String previousFormat,
                                    String destinationFormat) {

        String formattedDateTime = null;

        try {
            DateFormat dateFormat = new SimpleDateFormat(previousFormat);
            Date date = dateFormat.parse(dateTime);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(destinationFormat);
            formattedDateTime = simpleDateFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return formattedDateTime;

    }

但我得到以下错误异常,

10-16 18:34:45.023 15313-15313/id.co.impilo.patient W/System.err: java.text.ParseException: Unparseable date: "16 Oktober 2016" (at offset 3)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at java.text.DateFormat.parse(DateFormat.java:579)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at id.co.impilo.patient.helper.Helper.convertDateFormat(Helper.java:171)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at id.co.impilo.patient.activities.ActivityAddPatient.callAddPatientAPI(ActivityAddPatient.java:832)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at id.co.impilo.patient.activities.ActivityAddPatient.addPatient(ActivityAddPatient.java:302)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at id.co.impilo.patient.activities.ActivityAddPatient_ViewBinding$4.doClick(ActivityAddPatient_ViewBinding.java:87)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at android.view.View.performClick(View.java:5217)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at android.view.View$PerformClick.run(View.java:21342)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at android.os.Looper.loop(Looper.java:148)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5551)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
10-16 18:34:45.028 15313-15313/id.co.impilo.patient W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

1 个答案:

答案 0 :(得分:0)

我通过添加Locale.getDefault()来编辑我的代码来解决问题,

public static String convertDateFormat(String dateTime, String previousFormat,
                                    String destinationFormat) {

        String formattedDateTime = null;

        try {
            DateFormat dateFormat = new SimpleDateFormat(previousFormat, Locale.getDefault());
            Date date = dateFormat.parse(dateTime);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(destinationFormat, Locale.getDefault());
            formattedDateTime = simpleDateFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return formattedDateTime;

    }