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.

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 here Commented Jul 2, 2013 at 18:12
  • SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); Commented Jul 3, 2013 at 11:58
  • 1
    private String getDateString(long timeInMilliseconds) { SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy 'at' HH:mm:ss z"); return formatter.format(timeInMilliseconds); } Commented Jan 6, 2020 at 18:33

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

8 Comments

Modified, thanks @fvu
this answer is better, because author also give an example how to respect time-zones.
Excellent! Btw to get current time zone of the device we can use TimeZone.getDefault()
When not specified, default timezone is used anyways
@Nicolas I just added the package namespaces for the clases used.
|
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.

2 Comments

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…).
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.
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);

1 Comment

note that the question is to convert epoch minutes to date, and then formtat it to a pattern with a timezone offset

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.