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.

Sarwar Sateer
  • 395
  • 3
  • 16
bigData
  • 1,318
  • 4
  • 16
  • 27
  • `DateTime whatever = new DateTime(yourunixtimestampaslong * 1000L, DateTimeZone.UTC);` if you use [JodaTime](http://joda-time.sourceforge.net/). Or `DateTime whatever = new DateTime(yourunixtimestampaslong * 1000L, DateTimeZone.forOffsetHours(-4));` for your second example. Javadoc [here](http://joda-time.sourceforge.net/apidocs/index.html) – fvu Jul 02 '13 at 18:12
  • SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); – Ugur Artun Jul 03 '13 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); }` – JVJplus Jan 06 '20 at 18:33

3 Answers3

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)
David Hofmann
  • 5,683
  • 12
  • 50
  • 78
  • Modified, thanks @fvu – David Hofmann Jul 03 '13 at 11:56
  • 2
    this answer is better, because author also give an example how to respect time-zones. – drdrej May 21 '14 at 10:01
  • 2
    Excellent! Btw to get current time zone of the device we can use `TimeZone.getDefault()` – Atul Jun 24 '16 at 20:55
  • 2
    When not specified, default timezone is used anyways – David Hofmann Jun 24 '16 at 21:23
  • 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.sql` – Nicolas Mar 11 '17 at 18:59
  • @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. – Matthew Bahr Mar 14 '18 at 18:42
  • 1
    @Nicolas I just added the package namespaces for the clases used. – David Hofmann Mar 15 '18 at 18:56
  • @DavidHofmann, if i use default time will it change time for different countries? – Jay Dangar Sep 17 '18 at 08:23
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.

Jonathan
  • 20,053
  • 6
  • 63
  • 70
  • Much of the java.time functionality is back-ported to Java 6 & 7 in [ThreeTen-Backport](http://www.threeten.org/threetenbp/) and further adapted to [Android](https://en.wikipedia.org/wiki/Android_(operating_system)) in [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) (see [*How to use…*](http://stackoverflow.com/q/38922754/642706)). – Basil Bourque Mar 16 '17 at 06:10
  • Your example of an offset-from-UTC (`GMT-4`) is more appropriate to [`OffsetDateTime`](https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html) than `ZonedDateTime`. Also, I suggest you break out your example code into pieces to introduce each step and each idea separately. – Basil Bourque Mar 16 '17 at 06:12
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);
andre.barata
  • 663
  • 3
  • 11