2013-12-18 63 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); 
} 
相关问题