简体   繁体   中英

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!!! it should be 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

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. So, you need to convert the Unix time into milliseconds and then pass the same to 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. It is recommended to stop using it completely and switch to java.time , the modern date-time API * .

Solution using java.time , the modern API:

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 .

Solution using the legacy API:

Avoid performing calculations yourself if there is an OOTB (Out-Of-The-Box) API available for it eg 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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