1

我正在尝试转换以下纪元时间

1382364283

将其插入在线转换器时,它会给我正确的结果。

2013 年 10 月 21 日 15:00:28

但是下面的代码

    Long sTime = someTime/1000;
    int test = sTime.intValue();  // Doing this to remove the decimal

    Date date = new Date(test);
    DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    String formatted = format.format(date);

    this.doorTime = formatted;

退货

16/01/1970 17:59:24

我已经尝试了其他几种方法来转换它,有什么我遗漏的吗?

4

2 回答 2

4

纪元时间是自纪元以来的秒数。你将它除以一千,得到千秒数,即千秒。但是采用的参数Date是以毫秒为单位的。您的代码应该是:

    long someTime = 1382364283;
    long sTime = someTime*1000;  // multiply by 1000, not divide

    Date date = new Date(sTime);
    DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    String formatted = format.format(date);

    System.out.println(formatted); // 21/10/2013 10:04:43

我没有得到你的确切结果,但我得到的结果与我尝试过的在线转换器相同。

于 2013-10-21T15:11:11.380 回答
1

构造函数 Date(long time) 需要以毫秒为单位的时间!当您将您的 someTime(可能以毫秒为单位)除以 1000 时,您将获得以秒为单位的时间,而不是毫秒。

于 2013-10-21T15:12:36.477 回答