【问题标题】:Epoch to Date Not Working Properly迄今为止无法正常工作的纪元
【发布时间】:2014-08-24 20:03:36
【问题描述】:

我正在尝试使用 SimpleDateFormat 进行简单的历元到日期转换。每当我运行程序时,我都会不断收到转换后的无效日期。我已经在转换器中运行了时代标记[并且我还将日期存储在外部服务器中],所以我知道输出应该是什么,但我仍然收到错误的转换。我在实验中尝试了对 long epochTime 的多项数学更改,但无济于事。我确实知道 [或至少我很确定] 我的纪元时间戳以毫秒为单位存储,因此需要 * 1000 更改。这是我所拥有的:

String convertedTime = null;

//(1) //startTime = startTime/1000;  --none of these three manipulations solve the issue
//(2) //startTime = startTime * 1000;
//(3) //long change = (long) startTime * 1000;

Date d = newDate(startTime); //startTime is *long* parameter passed into method

SimpleDateFormat formatter = new SimpleDateFormat("mm-dd-yyyy 'at' hh:mm:ss");
convertedTime= formatter.format(d);

例如,如果纪元标记是 1404327407738 并且我不包括 (1)、(2)、(3) 我在 01:56 收到 56-02-2014:下午 47 点

  • 包括 (1) - 05-17-1970 上午 12:05:27
  • 包括 (2) - 05:35:38 AM 35-13-46471
  • 包括 (3) - 上午 05:35:38 的 35-13-46471(是的,我在此处替换了 startTime 的更改)

应该收到 07-02-2014 下午 01:56:47

【问题讨论】:

    标签: java date simpledateformat epoch


    【解决方案1】:

    格式字符串应为“MM-dd-yyyy 'at' hh:mm:ss”。

    “MM”代表年中的月份,“mm”代表小时中的分钟(请参阅http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)。

    【讨论】:

      【解决方案2】:

      应该是MM:

      SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy 'at' hh:mm:ss");
      

      【讨论】:

        猜你喜欢
        • 2021-07-15
        • 1970-01-01
        • 2013-01-15
        • 2017-08-14
        • 1970-01-01
        • 2014-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多