转换日期格式的Moto G2 Marshmallow有异常

时间:2016-02-18 08:27:25

标签: simpledateformat android-6.0-marshmallow

我将简单的日期格式应用于我的日期字符串。在棒棒糖和棉花糖版本的Moto G3中,这种转变效果很好,但它在Moto G2中引发了一些例外。

这是我的代码:

SimpleDateFormat fmt;

        if(dateString.contains(":"))
        {
            fmt = new SimpleDateFormat("hh:mm a");
        }
        else
           fmt = new SimpleDateFormat("hh a");

        Date date = null;
        try {
            date = fmt.parse(dateString);
        } catch (ParseException e) {

            e.printStackTrace();

        }

        SimpleDateFormat fmtOut = new SimpleDateFormat("HH:mm:ss");
        return fmtOut.format(date);

Log Cat:

java.text.ParseException: Unparseable date: "11 AM" (at offset 3)
02-18 13:48:21.886 1562-2096/com.example.k W/System.err:     at java.text.DateFormat.parse(DateFormat.java:579)
02-18 13:48:21.886 1562-2096/ccom.example.k  W/System.err:     at com.braintech.kwikgrocery.utils.Utility.convertTimeFormat(Utility.java:236)
02-18 13:48:21.886 1562-2096/com.example.k  W/System.err:     at com.braintech.kwikgrocery.activity.CheckOutConfirmOrderActivity$GetTimeSlots.doInBackground(CheckOutConfirmOrderActivity.java:289)
02-18 13:48:21.886 1562-2096/com.example.k  W/System.err:     at com.braintech.kwikgrocery.activity.CheckOutConfirmOrderActivity$GetTimeSlots.doInBackground(CheckOutConfirmOrderActivity.java:239)
02-18 13:48:21.886 1562-2096/com.example.k  W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
02-18 13:48:21.886 1562-2096/com.example.k  W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-18 13:48:21.886 1562-2096/com.example.k W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
02-18 13:48:21.886 1562-2096/com.example.k  W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
02-18 13:48:21.886 1562-2096/com.example.k  W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
02-18 13:48:21.886 1562-2096/com.example.k  W/System.err:     at java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:4)

调用字符串上的trim并替换

 SimpleDateFormat fmtOut = new SimpleDateFormat("HH:mm:ss");

使用

 SimpleDateFormat fmtOut = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);