Convert unix timestamp to date in java [duplicate]
Asked Answered
K

3

70

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.

Karlykarlyn answered 2/7, 2013 at 18:7 Comment(3)
DateTime whatever = new DateTime(yourunixtimestampaslong * 1000L, DateTimeZone.UTC); if you use JodaTime. Or DateTime whatever = new DateTime(yourunixtimestampaslong * 1000L, DateTimeZone.forOffsetHours(-4)); for your second example. Javadoc hereHornmad
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");Voile
private String getDateString(long timeInMilliseconds) { SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy 'at' HH:mm:ss z"); return formatter.format(timeInMilliseconds); }Cockspur
T
169

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)
Truditrudie answered 2/7, 2013 at 18:22 Comment(8)
Modified, thanks @HornmadTruditrudie
this answer is better, because author also give an example how to respect time-zones.Junko
Excellent! Btw to get current time zone of the device we can use TimeZone.getDefault()Sander
When not specified, default timezone is used anywaysTruditrudie
Which package do I have to import for Date? Visual Eclipse gives me several options for Java 8, and none of them work: sun.util.calendar.BaseCalendar, sun.util.calendar.LocalGregorianCalendar, java.util and java.sqlBohun
@Nicolas: I'm assuming you've figured it out by now but java.util.Date is what is used in this example along with java.text.SimpleDateFormat. If you're using Java 8 you can use the below example (Instant.ofEpochSecond) but be aware it requires Android O as well.Microclimate
@Bohun I just added the package namespaces for the clases used.Truditrudie
@DavidHofmann, if i use default time will it change time for different countries?Heaviness
M
41

Java 8 introduces the Instant.ofEpochSecond utility method for creating an Instant from a Unix timestamp, this can then be converted into a ZonedDateTime 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.

Maryjomaryl answered 5/12, 2014 at 15:52 Comment(2)
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).Cleavage
Your example of an offset-from-UTC (GMT-4) is more appropriate to OffsetDateTime than ZonedDateTime. Also, I suggest you break out your example code into pieces to introduce each step and each idea separately.Cleavage
S
12

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

java.util.Date dateTime=new java.util.Date((long)timeStamp*1000);
Strangury answered 2/7, 2013 at 18:12 Comment(1)
note that the question is to convert epoch minutes to date, and then formtat it to a pattern with a timezone offsetTruditrudie

© 2022 - 2024 — McMap. All rights reserved.