103

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().

Thalaivar
  • 23,282
  • 5
  • 60
  • 71
Xenph Yan
  • 83,019
  • 16
  • 48
  • 55
  • See [my detailed answer](http://stackoverflow.com/a/20370992/642706) to a similar question on converting between milliseconds to date-time with example code using Joda-Time library. – Basil Bourque Dec 06 '13 at 04:12
  • `private String getDateString(long timeInMilliseconds) { SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy 'at' HH:mm:ss z"); return formatter.format(timeInMilliseconds); } ` – JVJplus Jan 06 '20 at 18:33

6 Answers6

148

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);
Community
  • 1
  • 1
Marc Novakowski
  • 44,628
  • 11
  • 58
  • 63
  • 4
    Wouldn't this technically be incorrect as it doesn't take into account leap seconds? – Maciej Piechotka Oct 29 '11 at 13:23
  • 1
    Maciej, op's "seconds-based" epoch already accounts for leap seconds, it is "seconds"-based. So this answer is correct. – thecarpy Oct 21 '14 at 07:39
  • 1
    It gives me incorrect values if multiplied by 1000, isn't there a better way of doing it? – Ishaan Jul 30 '16 at 15:18
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Sep 27 '18 at 21:48
  • The question was since the epoch, which is UTC while date expects the long to be from GMT, are they they same? – Luke Feb 13 '20 at 01:35
45

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/

Ryan Emerle
  • 15,461
  • 8
  • 52
  • 69
  • 1
    This works a treat ...IDK Y this hasn't been checked as Answer :) – Makky Feb 28 '12 at 09:31
  • Thank you for this answer. I was sitting here trying to instantiate a date from an epoch and the result was always some day in 1970, and it was because I was unknowingly giving Java milliseconds, not seconds :) – Lo-Tan Oct 25 '12 at 17:44
34

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" );
Community
  • 1
  • 1
Przemek
  • 7,111
  • 3
  • 43
  • 52
  • Good answer. I would suggest dropping the `LocalDateTime` and just show a `ZonedDateTime`. In most business apps, `ZonedDateTime` is the best way to go. People tend to get themselves into trouble/confusion with the `LocalXXX` types. – Basil Bourque Dec 24 '15 at 04:15
10
long timestamp = Long.parseLong(date)
Date expiry = new Date(timestamp * 1000)
tcurdt
  • 14,518
  • 10
  • 57
  • 72
0

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)

-1

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)

Varkhan
  • 16,601
  • 5
  • 31
  • 24