【问题标题】:Converting epoch time stamp to readable date not working将纪元时间戳转换为可读日期不起作用
【发布时间】:2014-11-05 01:27:13
【问题描述】:

我正在尝试使用此代码转换我拥有的时间戳,但输出完全错误,输出为 17/01/1970 16:56:28!!!应该是 2014 年 8 月 7 日下午 5:14:59

Date date = new Date(1407388499);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
System.out.println(formatted);
format.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
formatted = format.format(date);
System.out.println(formatted);

请帮帮我

【问题讨论】:

  • 日期预计自纪元以来的毫秒数,而不是秒数。

标签: java android timestamp epoch


【解决方案1】:

你的日期不够长

new Date(1407388499);
Sat Jan 17 1970 15:56:28 GMT+0900 (Japan Standard Time)
new Date(1407388499000);
Thu Aug 07 2014 14:14:59 GMT+0900 (Japan Standard Time)

该值应该是 Long,即毫秒数

编辑

所以如果你收到的号码是

 int dt = 1407388499:

那你需要做的

Date date = new Date(1000L * dt);    

【讨论】:

  • 但那是我从网络服务收到的号码,所以我无法控制它,我将它作为字符串接收,然后我将其转换
  • 乘以 1000
  • 我已经编辑了我的答案。只要确保你的 int 被强制转换为 long
【解决方案2】:

java.time

问题的根本原因是Unix time 指定自纪元以来的,而java.util.Date(long date) 期望自纪元以来的毫秒数。因此,您需要将 Unix 时间转换为毫秒,然后将其传递给 java.util.Date(long date)

但是,旧的日期时间 API(java.util 日期时间类型及其格式类型,SimpleDateFormat 等)已过时且容易出错。建议完全停止使用,改用java.timemodern date-time API*

使用现代 API java.time 的解决方案:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        Instant instant = Instant.ofEpochSecond(1407388499);

        // Corresponding date-time in Australia/Sydney
        ZonedDateTime zdtSydney = instant.atZone(ZoneId.of("Australia/Sydney"));
        System.out.println(zdtSydney);

        // Formatted
        System.out.println(DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm:ss", Locale.ENGLISH).format(zdtSydney));
    }
}

输出:

2014-08-07T15:14:59+10:00[Australia/Sydney]
07/08/2014 15:14:59

Trail: Date Time了解更多关于java.timemodern date-time API*

使用旧版 API 的解决方案:

如果有可用的 OOTB(即开即用)API,例如,请避免自己执行计算。 TimeUnit#convert.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        Date date = new Date(TimeUnit.MILLISECONDS.convert(1407388499, TimeUnit.SECONDS));

        DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        format.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
        System.out.println(format.format(date));
    }
}

输出:

07/08/2014 15:14:59

* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport,它将大部分 java.time 功能向后移植到 Java 6 和 7 . 如果您正在为一个 Android 项目工作,并且您的 Android API 级别仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaringHow to use ThreeTenABP in Android Project

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-12
    • 2018-08-03
    • 2019-05-12
    • 1970-01-01
    • 2022-08-04
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多