2013-02-27 53 views
1

將日期轉換爲不同格式的字符串時遇到問題。日期:使用simpledateformat將日期轉換爲字符串

lastDownloadDate>>Wed Feb 27 16:20:23 IST 2013 
lastChangeDate>>Wed Feb 27 15:11:00 IST 2013 

我想將此格式轉換爲另一種格式:yyyy-mm-dd HH:mm:ss

當我試圖把它轉換我用得到不同的結果:

DateFormat outputFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss"); 


String lastDownloadTimeVal = outputFormat.format(lastDownloadDate); 
System.out.println("lastDownloadTimeVal>>"+lastDownloadTimeVal); 
String lastChangeTimeVal = outputFormat.format(lastChangeDate); 
System.out.println("lastChangeTimeVal>>"+lastChangeTimeVal); 

我得到錯誤的結果,月,正在替換分鐘:

lastDownloadTimeVal>>2013-20-27 16:20:23 
lastChangeTimeVal>>2013-11-27 15:11:00 
+2

月份模式MM不毫米 – 2013-02-27 11:39:24

+0

@MiguelPrz只有一個解釋,爲什麼OP應該按月而非使用的M M。 +1。如果這是一個答案,我只給你一個贊成票。 – 2013-02-27 11:40:12

回答

1

yyyy-mm-dd HH:mm:ss這是錯的。這應該是yyyy-MM-dd HH:mm:ss。 有關將日期從一種格式轉換爲另一種格式的更多詳細信息,請參閱http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

+1

哈哈,快15秒! upvoting你。 – vikingsteve 2013-02-27 11:39:22

+0

感謝您的答案。 – user2057006 2013-02-27 11:41:39

+0

這是我的錯字問題。代替MM,我在代碼中匆忙地使用了mm。 :( – user2057006 2013-02-27 11:43:25

0

嘗試

DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
1

你使用了錯誤的格式一個月。

yyyy-mm-dd HH:mm:ss ----> yyyy-MM-dd HH:mm:ss 
mm--->Minutes. 
MM--->Months 
0

變化

DateFormat outputFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss"); 

DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
0
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
mm for Minutes 
MM for Months