android - 将 unix/epoch 时间转换为格式化日期 - 意外日期

标签 android timestamp android-date

我正在尝试将时间戳转换为人类可读的日期和时间。我试过了:

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/

相关文章:

MySQL-日期-时间戳

android - 如何在android中将日期从12小时格式转换为24小时格式

android - 单击带有 RecyclerView 和 DataBinding 的项目后如何调用导航操作?

android - 使用可观察的 ScrollView 时,如何让我的选项卡 View 填满整个屏幕?

mysql - 每次创建表时间戳时都会自动添加 ON UPDATE

python - 由 Timestamp 对象组成的 Pandas 系列的 min() 和 max() 方法的意外结果

java - close() Cursor 对象然后为其设置新值是否可以?

android - 关于安卓国际化语言

Android - 获取日期到 textview

android - 将 UTC 时间格式转换为本地时间