我正在尝试将时间戳转换为人类可读的日期和时间。我试过了:
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
最佳答案
试试这个,
public static String Epoch2DateString(long epochSeconds, String formatString) {
Date updatedate = new Date(epochSeconds * 1000);
SimpleDateFormat format = new SimpleDateFormat(formatString);
return format.format(updatedate);
}
关于android - 将 unix/epoch 时间转换为格式化日期 - 意外日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20654967/