【问题标题】:Java: epoch date to MM/DD/YYYYJava:纪元日期为 MM/DD/YYYY
【发布时间】:2014-04-15 02:36:35
【问题描述】:

我的时间:1386696238

我的代码:

Date date = new Date(Long.parseLong(currentNotification.getDate_created()));
        SimpleDateFormat formatter = new SimpleDateFormat("MM/DD/yyyy", Locale.getDefault());
        String dateString = formatter.format(date);

我的结果:1/16/1970

想要的结果:12/10/2013

我做错了什么?

【问题讨论】:

  • 月日:d(小写)
  • @Reimeus 乘以 1000 在我的申请中给出了正确的日期
  • java.util 日期时间 API 及其格式化 API SimpleDateFormat 已过时且容易出错。建议完全停止使用它们并切换到modern Date-Time API。使用现代日期时间 API 检查this page 以获得答案。

标签: java date simpledateformat epoch


【解决方案1】:

您的值以秒为单位,因此您需要将其乘以 1000。

另外,DD 是一年中的某一天,你想要dd

SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault());

【讨论】:

    【解决方案2】:

    我想您应该将输入乘以 1000(UNIX 时间戳以秒为单位,而 Java j.u.Date 以毫秒为单位)。

    编辑:

    哦,您的代码中的另一个错误:模式符号应该是 d,而不是 D(一年中的某天)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 2017-07-20
      • 2018-03-31
      • 2014-05-30
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多