2017-01-25 105 views
0

我使用的是功能可按的Android無法解析日期:2017年1月6日

public static String getFormatedDate(String dateVal) { 
    String tempDate = null; 
    Locale systemLocale = Locale.getDefault(); 

    try { 
     SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy", 
       systemLocale); 
     Date date = (Date) formatter.parse(dateVal); 
     SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); 
     tempDate = sdf.format(date); 

    } catch (Exception e) { 
     ApplicationLog.log(TAG, e); 
    } 

    return tempDate; 
} 

在這個功能是否正常工作很多設備,但在設備之一,在分析日的異常被提出。這個異常是否與Locale.getDefault()有關?如果是,我應該做什麼改變?謝謝。

調用函數

Utilities.getFormatedDate(date); 

而例外的是...

Utilities:java.text.ParseException: Unparseable date: "Jan 06, 2017" (at offset 0) 
at java.text.DateFormat.parse(DateFormat.java:579) 
+0

你可以發佈整個堆棧跟蹤嗎? –

+0

你也可以分享你調用這個方法的代碼嗎? –

+0

是的,你是對的。只是嘗試不傳遞語言環境。 –

回答

0

您試圖解析從語言環境相關格式"MMM dd, yyyy"日期和設置系統語言環境。嘗試傳遞符合您的格式的語言環境。例如Locale.US。改變你的方法,

public static String getFormatedDate(String dateVal) { 
    String tempDate = null; 

    try { 
     SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy", 
       Locale.US); 
     Date date = (Date) formatter.parse(dateVal); 
     SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); 
     tempDate = sdf.format(date); 

    } catch (Exception e) { 
     ApplicationLog.log(TAG, e); 
    } 

    return tempDate; 
} 
相關問題