2017-01-25 32 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; 
} 
相关问题