Unix epoch time to Java Date object

JavaDateJava Time

Java Problem Overview


I have a string containing the UNIX Epoch time, and I need to convert it to a Java Date object.

String date = "1081157732";
DateFormat df = new SimpleDateFormat(""); // This line
try {
  Date expiry = df.parse(date);
 } catch (ParseException ex) {
  ex.getStackTrace();
}

The marked line is where I'm having trouble. I can't work out what the argument to SimpleDateFormat() should be, or even if I should be using SimpleDateFormat().

Java Solutions


Solution 1 - Java

How about just:

Date expiry = new Date(Long.parseLong(date));

EDIT: as per rde6173's answer and taking a closer look at the input specified in the question , "1081157732" appears to be a seconds-based epoch value so you'd want to multiply the long from parseLong() by 1000 to convert to milliseconds, which is what Java's Date constructor uses, so:

Date expiry = new Date(Long.parseLong(date) * 1000);

Solution 2 - Java

Epoch is the number of seconds since Jan 1, 1970..

So:

String epochString = "1081157732";
long epoch = Long.parseLong( epochString );
Date expiry = new Date( epoch * 1000 );

For more information: http://www.epochconverter.com/

Solution 3 - Java

java.time

Using the java.time framework built into Java 8 and later.

import java.time.LocalDateTime;
import java.time.Instant;
import java.time.ZoneId;

long epoch = Long.parseLong("1081157732");
Instant instant = Instant.ofEpochSecond(epoch);
ZonedDateTime.ofInstant(instant, ZoneOffset.UTC); # ZonedDateTime = 2004-04-05T09:35:32Z[UTC]

In this case you should better use ZonedDateTime to mark it as date in UTC time zone because Epoch is defined in UTC in Unix time used by Java.

ZoneOffset contains a handy constant for the UTC time zone, as seen in last line above. Its superclass, ZoneId can be used to adjust into other time zones.

ZoneId zoneId = ZoneId.of( "America/Montreal" );

Solution 4 - Java

long timestamp = Long.parseLong(date)
Date expiry = new Date(timestamp * 1000)

Solution 5 - Java

Better yet, use JodaTime. Much easier to parse strings and into strings. Is thread safe as well. Worth the time it will take you to implement it.

Solution 6 - Java

To convert seconds time stamp to millisecond time stamp. You could use the TimeUnit API and neat like this.

long milliSecondTimeStamp = MILLISECONDS.convert(secondsTimeStamp, SECONDS)

Solution 7 - Java

Hum.... if I am not mistaken, the UNIX Epoch time is actually the same thing as

System.currentTimeMillis()

So writing

try {
    Date expiry = new Date(Long.parseLong(date));
}
catch(NumberFormatException e) {
    // ...
}

should work (and be much faster that date parsing)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionXenph YanView Question on Stackoverflow
Solution 1 - JavaMarc NovakowskiView Answer on Stackoverflow
Solution 2 - JavaRyan EmerleView Answer on Stackoverflow
Solution 3 - JavaPrzemekView Answer on Stackoverflow
Solution 4 - JavatcurdtView Answer on Stackoverflow
Solution 5 - JavaWolfmanDragonView Answer on Stackoverflow
Solution 6 - JavaDinesh JayabalanView Answer on Stackoverflow
Solution 7 - JavaVarkhanView Answer on Stackoverflow