你必須首先parse
的String
使用parse
方法從SimpleDateFormat
。
然後通過由parse
方法返回到另一個SimpleDateFormat
,然後使用format
方法得到你想要的格式的日期Date
對象。
String s = "201307061230202";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS"); // format in which you get the String
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // format in which you want the date to be in
System.out.println(sdf1.format(sdf.parse(s)));
的HH
,hh
,KK
和kk
在小時字段中的意義是不同的。我已經使用HH
您可以根據您的要求使用。
H Hour in day (0-23)
k Hour in day (1-24)
K Hour in am/pm (0-11)
h Hour in am/pm (1-12)
來源
2013-07-06 21:06:33
JHS
你到目前爲止嘗試了什麼? –
我已經嘗試過了,但是我得到了以下錯誤:java.text.ParseException:Unparseable date:「20130706234310」 – Liviu
我試過SimpleDateFormat和DateTimeFormat – Liviu