简体   繁体   English

将纪元时间戳转换为可读日期不起作用

[英]Converting epoch time stamp to readable date not working

I'm trying to use this code to convert the timestamp i have but the output is completely wrong, the output is 17/01/1970 16:56:28!!!我正在尝试使用此代码来转换我拥有的时间戳,但输出完全错误,输出为 17/01/1970 16:56:28 !!! it should be 8/7/2014 5:14:59 PM应该是 8/7/2014 5:14:59 PM

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);

Help me please请帮帮我

Your date is not long enough你的约会时间不够长

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)

The value should be a Long that is the number of millseconds该值应该是一个 Long,表示毫秒数

Edit编辑

So if your received number is所以如果你收到的号码是

 int dt = 1407388499:

Then you need to do然后你需要做

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

java.time时间

The root cause of the problem is that the Unix time specifies seconds since the Epoch whereas java.util.Date(long date) expects the number of milliseconds since the Epoch.问题的根本原因是Unix 时间指定自纪元以来的秒数,而java.util.Date(long date)期望自纪元以来的毫秒数。 So, you need to convert the Unix time into milliseconds and then pass the same to java.util.Date(long date) .因此,您需要将 Unix 时间转换为毫秒,然后将其传递给java.util.Date(long date)

However, the legacy date-time API ( java.util date-time types and their formatting type, SimpleDateFormat etc.) is outdated and error-prone.但是,遗留的日期时间 API( java.util日期时间类型及其格式类型, SimpleDateFormat等)已经过时且容易出错。 It is recommended to stop using it completely and switch to java.time , the modern date-time API * .建议完全停止使用它并切换到java.time现代日期时间 API *

Solution using java.time , the modern 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));
    }
}

Output:输出:

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

Learn more about java.time , the modern date-time API * from Trail: Date Time .Trail: Date Time 中了解有关java.time更多信息, 现代日期时间 API *

Solution using the legacy API:使用旧 API 的解决方案:

Avoid performing calculations yourself if there is an OOTB (Out-Of-The-Box) API available for it eg TimeUnit#convert .如果有可用的 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));
    }
}

Output:输出:

07/08/2014 15:14:59

* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project . * 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用ThreeTen-Backport,它将大部分java.time功能向后移植到 Java 6 和 7。如果您正在为 Android 项目和您的 Android API 工作级别仍然不符合 Java-8,请检查 通过 desugaringHow to use ThreeTenABP in Android Project 可用的 Java 8+ APIs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM