2015-09-30 27 views
2

我想解析日期表示爲一個字符串。字符串的一個例子是:20150724T104139.118+02Dateformat不會解析字符串

有解析代碼:

DateFormat formatter = new SimpleDateFormat(timestampFormat); 
return formatter.parse("yyyyMMdd'T'HHmmss'.'SX"); 

其拋出錯誤:

java.text.ParseException: Unparseable date: "20150724T104139.118+02" 
at java.text.DateFormat.parse(DateFormat.java:366) 
at 

的圖案是轉化日期成,當我使用相同的串。

回答

2

首先,你錯誤地交換了SimpleDateFormat字符串,要分析之日起的字符串。格式字符串也與示例日期不匹配。你需要三次毫秒數的S。請嘗試以下操作:

String text = "20150724T104139.118+02"; 
DateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmss'.'SSSX"); 
Date myTime = formatter.parse(text); 
+0

感謝,單獨的S是錯誤的問題複製代碼(我簡化了一點)當字符串交換髮生這在原文中是正確的。 – olkoza

2

parse期待的日期,而不是模式:

所有的
new SimpleDateFormat(datePattern).parse(dateToParse) 
0

你可以嘗試這樣的:

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'.'sssX"); 
sdf.setTimeZone(TimeZone.getTimeZone("GMT")); //Set the timezone as per your env if you want 
sdf.parse("yourDate");