2017-01-25 6 views
0

내가 사용하고있어 funciton이 기능이 제대로 작동 많은 장치에서안드로이드 구문 분석 할 날짜 : 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; 
}