2013-12-18 67 views
0

我試圖將時間戳轉換爲人類可讀的日期和時間。我想:將unix/epoch時間轉換爲格式化日期 - 意外日期

String dateSt = 1386580621268; 
Log.i("*****", "date st is = "+dateSt); 
long unixSeconds = Long.parseLong(dateSt); 
Log.i("*******", "unix seconds is = "+unixSeconds); 
Date date = new Date(unixSeconds*1000L); // *1000 is to convert seconds to milliseconds 
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); // the format of your date 
String formattedDate = sdf.format(date); 
System.out.println(formattedDate); 

預期的結果是要09-12-2013,但是我得到28-12-45908。上面的例子可以發現於:Convert Unix timestamp to date java, answer by David Hofmann

回答

2

1386580621268不是自從劃時代劃時代爲2013年9月12日,但毫秒的Unix時間戳即秒。刪除*1000L或將輸入除以1000.

+0

啊我看到了,非常感謝你清除那個。它現在正常工作:)我剛剛刪除了像你所建議的'* 1000L'。再次感謝。 – DevC

10

嘗試此,

public static String Epoch2DateString(long epochSeconds, String formatString) { 
    Date updatedate = new Date(epochSeconds * 1000); 
    SimpleDateFormat format = new SimpleDateFormat(formatString); 
    return format.format(updatedate); 
} 
相關問題