2012-04-23 509 views
3

我得到日期字符串 - 「2012-04-19 20:51:06」。然後我通過SimpleDateFormat獲得毫時間。Java SimpleDateFormat - 輸入和輸出不同

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
    Date d=sdf.parse("2012-04-19 20:51:06"); 
    long milliTime=d.getTime() 

然後milliTime是1334839866000L

然而,當我轉換milliTime到日期格式字符串。結果是「2012-04-19 08:51:06」

long time = 1334839866000L; 
    Date date = new Date(time); 

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
    String t = sdf.format(date); 

什麼問題?

回答

8

嘗試使用HH作爲小時分量而不是hh。

+1

+1。區別在於24小時制(HH)和am/pm(hh)。 – Thilo 2012-04-23 03:04:53

+0

太棒了!非常感謝你:) – Goleo8 2012-04-23 03:06:13

+0

但是,不應該一個格式(hh?)拋出一個異常,當輸入錯誤的輸入(20)? – 2012-04-23 05:20:06