2014-08-29 106 views
0

嗨我想解析一個字符串到日期,但它是拋出異常java.text.ParseException: Unparseable date: "1409239380000" (at offset 13)。這裏是我的代碼:ParseException解析字符串日期

String text="1409239380000"; 
try{ 
     SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     Date date1=dateFormat.parse(text); 
    }catch(Exception e){ 
     Log.e("Error final:", e.toString()); 
    } 
+4

那麼,'1409239380000'確實不遵循格式'yyyy-MM-dd HH:mm:ss' ... – Keppil 2014-08-29 12:43:37

+0

我知道它,但我不知道如何將格式更改爲我擁有的格式。 – temerariomalaga 2014-08-29 12:44:57

+0

'1409239380000'是'Unparseable date',因此是'ParseException'。現在你的問題是什麼? – Unihedron 2014-08-29 12:45:16

回答

3

無需解析任何東西。你可以創建一個新的Date

String text = "1409239380000"; 
Date d = new Date(Long.valueOf(text)); 

如果你正在嘗試做的是格式化的方式指定的日期,使用format()

String text = "1409239380000"; 
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
System.out.println(dateFormat.format(new Date(Long.valueOf(text)))); 

這將打印

2014-08-28 17:23:00 
+0

好的非常感謝你。 – temerariomalaga 2014-08-29 12:53:55