我试图将时间戳转换为人类可读的日期和时间。我试过了:
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
答案 0 :(得分:14)
试试这个,
public static String Epoch2DateString(long epochSeconds, String formatString) {
Date updatedate = new Date(epochSeconds * 1000);
SimpleDateFormat format = new SimpleDateFormat(formatString);
return format.format(updatedate);
}
答案 1 :(得分:3)
1386580621268
不是unix时间戳,即自9-12-2013纪元以来的秒数,但是自纪元以来的毫秒数。删除*1000L
或将输入除以1000。
答案 2 :(得分:1)
我这样解决了。
public static String Epoch2DateString(String epochSeconds) {
Date updatedate = new Date(Integer.parseInt(epochSeconds) * 1000L);
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
return format.format(updatedate);
}