Android parseException unparseable date in specifi

2019-07-21 04:26发布

In my android App I am converting a string to a date using the following method

public Date convertToDate(String date) {  //(input date format "Feb 18, 2013 01:32 AM")
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
        try {  
            Date dateObj = dateFormat.parse(date);
            return dateObj;
        } catch (ParseException e) {
            e.printStackTrace();  
        } catch (Exception e) {
            Log.d("Utility", e.getMessage());
        }
        return null;
    }

It works fine in most of the mobile but for some reason it's not working in 2.2 version devices and some samsung devies and throwing parse Exception unparseable date. Please help.

1条回答
虎瘦雄心在
2楼-- · 2019-07-21 05:08

use simpledateformat for your code..

 SimpleDateFormat dfDate  = new SimpleDateFormat("MMM dd,yyyy HH:mm a");
 try {
    dateObj  = dfDate.parse(date);
} catch (java.text.ParseException e) {
    e.printStackTrace();
}
查看更多
登录 后发表回答