Convert unix timestamp to date in java

JavaUnix Timestamp

Java Problem Overview


How can I convert minutes from Unix timestamp to date and time in java? For example, timestamp 1372339860 correspond to Thu, 27 Jun 2013 13:31:00 GMT.

I want to convert 1372339860 to 2013-06-27 13:31:00 GMT.

Edit: Actually I want it to be according to US timing GMT-4, so it will be 2013-06-27 09:31:00.

Java Solutions


Solution 1 - Java

You can use SimlpeDateFormat to format your date like this:

long unixSeconds = 1372339860;
// convert seconds to milliseconds
Date date = new java.util.Date(unixSeconds*1000L); 
// the format of your date
SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); 
// give a timezone reference for formatting (see comment at the bottom)
sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT-4")); 
String formattedDate = sdf.format(date);
System.out.println(formattedDate);

The pattern that SimpleDateFormat takes if very flexible, you can check in the javadocs all the variations you can use to produce different formatting based on the patterns you write given a specific Date. http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

  • Because a Date provides a getTime() method that returns the milliseconds since EPOC, it is required that you give to SimpleDateFormat a timezone to format the date properly acording to your timezone, otherwise it will use the default timezone of the JVM (which if well configured will anyways be right)

Solution 2 - Java

Java 8 introduces the [Instant.ofEpochSecond][1] utility method for creating an [Instant][2] from a Unix timestamp, this can then be converted into a [ZonedDateTime][3] and finally formatted, e.g.:

final DateTimeFormatter formatter = 
    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

final long unixTime = 1372339860;
final String formattedDtm = Instant.ofEpochSecond(unixTime)
        .atZone(ZoneId.of("GMT-4"))
        .format(formatter);

System.out.println(formattedDtm);   // => '2013-06-27 09:31:00'

I thought this might be useful for people who are using Java 8.

[1]: https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#ofEpochSecond-long- "Instant.ofEpochSecond JavaDoc" [2]: https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html "Instant JavaDoc" [3]: https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html "ZonedDataTime JavaDoc"

Solution 3 - Java

You need to convert it to milliseconds by multiplying the timestamp by 1000:

java.util.Date dateTime=new java.util.Date((long)timeStamp*1000);

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
QuestionbigDataView Question on Stackoverflow
Solution 1 - JavaDavid HofmannView Answer on Stackoverflow
Solution 2 - JavaJonathanView Answer on Stackoverflow
Solution 3 - Javaandre.barataView Answer on Stackoverflow