2016-04-08 93 views
0

我得到這個錯誤:SimpleDate格式解析錯誤

java.text.ParseException: Unparseable date: "Fri Apr 08 2016 00:00:00 GMT+0530 (IST)" 

我已經使用這個SimpleDateFormat任何一個可以建議我一個正確的?

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z (Z)"); 
+1

注:'IST'也是'愛爾蘭標準Time','伊朗標準Time'和'以色列標準時間'我建議你最後放棄'(IST)'。 –

+0

@Ravi:你檢查我的答案嗎?如果它爲你工作,那麼接受+ upvote它。 – Unknown

+0

不!老兄它不適合我這只是給演員解析錯誤 –

回答

1

如果您的飼餵日期爲「2016年4月8日00:00:00 GMT + 0530(IST)」。那就錯了。請刪除GMT。所有時區僅根據GMT計算。

嘗試通過日期爲「週五2016年4月08日00:00:00 +0530(IST)」。它會工作。

+0

如果它已經工作,那麼請接受答案。謝謝。 –

+1

建議更改輸入通常不起作用。解決方案通常是調整模式,而不是可能來自用戶無權更改的外部來源的輸入。 –

1

正確的語法分析日期字符串應該是:

Fri Apr 08 2016 00:00:00 IST (+0530) 

這個小片段應該清除混亂。這是你在做什麼相反:

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z (Z)"); 

String strDate = format.format(new Date()); 
System.out.println(strDate); 

輸出是:Fri Apr 08 2016 17:26:34 IST (+0530)

1

你可以嘗試模式EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)

1)使用Java 1.6:

System.out.println(fromStringToDate("Fri Apr 08 2016 00:00:00 GMT+0530 (IST)", "EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)")); 

Output (in my system) : Fri Apr 08 00:00:00 IST 2016

請參考此鏈接瞭解時區值Java TimeZone List

public static Date fromStringToDate(String myPotentialDate,String pattern) throws Exception{ 
     // DateFormat myDateFormat = new SimpleDateFormat(pattern); 
     String countryCode = "US"; 
     String languageCode = "en"; 
     String timeZone = "Asia/Kolkata"; 
     DateFormat myDateFormat = getDateFormat(pattern,countryCode,languageCode,timeZone); 
     // We set the Leniant to false 
     myDateFormat.setLenient(false); 
     try { 
      return myDateFormat.parse(myPotentialDate); 
     } 
     catch (ParseException e) { 
      // Unparsable date 
      throw new Exception("Unparsable date '"+myPotentialDate+"' with pattern '"+pattern+"'. Due to '"+e+"'",e); 
     } 

    } 

    private static DateFormat getDateFormat(String pattern,String countryCode,String languageCode,String timeZoneId){ 
     // We build the Local 
     Locale myLocale = new Locale(languageCode,countryCode); 
     // We build the DateFormat with the Local and the pattern 
     DateFormat myDateFormat = new SimpleDateFormat(pattern,myLocale); 
     // We set the TimeZone to the correct one 
     myDateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneId)); 
     // We set the Leniant to false 
     myDateFormat.setLenient(false); 
     return myDateFormat; 
    } 

2)使用Java 1.8 Java8 Date time API

 String countryCode = "US"; 
     String languageCode = "en"; 
     String timeZoneId = "Asia/Kolkata"; 

     LocalDateTime dt = LocalDateTime.parse("Fri Apr 08 2016 00:00:00 GMT+0530 (IST)", 
       DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)").withLocale(new Locale(languageCode,countryCode))); 
     ZoneId zoneId= ZoneId.of(timeZoneId); 
     ZonedDateTime zdt= ZonedDateTime.of(dt, zoneId); 
     System.out.println(zdt); 

輸出:

2016-04-08T00:00+05:30[Asia/Kolkata]

+0

嗯,我無法重現您的輸出「Fri Apr 08 00:00:00 IST 2016」。相反,我在歐洲/柏林時區的「時間2016年4月8日00:00:00 CEST 2016」中獲得了這個時間,這個時間距離印度還有3個半小時。所以結果是錯誤的。你真的測試過你的代碼嗎? –

+0

嗨。 OP問他無法解析日期(Fri Apr 08 2016 00:00:00 GMT + 0530(IST))。所以我的答案只是解析給定的字符串。我已經把輸出結果告訴了他我能夠解析日期。 – Unknown

+0

@Meno Hochschild:請參閱我的更新回答 – Unknown