2015-09-03 190 views
3

我想從字符串日期,java.text.ParseException:無法解析日期:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); 
Date date = simpleDateFormat.parse(dateString); 

我的日期字符串是:

Wed, 02 September 2015 08:27:00 MSK 

我得到這個錯誤:

java.text.ParseException: Unparseable date: "Wed, 02 September 2015 08:27:00 MSK" (at offset 32) 

怎麼了? Thx尋求幫助。

回答

5

我看到你有MSK時間。您的語言環境是英語還是俄語?因爲如果你的語言環境是俄語,9月不是有效的月份。因此,您有兩種選擇:

  1. 將您的語言環境更改爲英語。
  2. 讓你的語言環境在俄羅斯,但寫在俄羅斯的月份。

修復了變更語言爲英語:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH); 
+0

謝謝你,這就是我所需要的。 – llaerto

1

確保你使用正確的語言環境。在這裏你有Wed, 02 September 2015 08:27:00 MSK作爲日期字符串,其中包括MSK這是俄羅斯,莫斯科的TimeZone。所以你需要設置TimeZone

試試這個:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); 
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("MCK")); 
Date date = simpleDateFormat.parse(dateString); 
相關問題