0

Hi i'm getting a dates and hours in timestamp format from a webservice and i'm trying to convert them in String to show them in my app

i've tried

Timestamp ts = new Timestamp(jsonObject.getLong("release_date"));

or

SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");   
Date date = new Date( jsonObject.getLong("release_date"));

and it always give me "1970-01-17 07:54:39.6" even if i get other timestamp

for example both 1407279600 and 1406674800 give me "1970-01-17"

any idea on how to do it ?

user3718160
  • 481
  • 2
  • 11
  • 23
  • possible duplicate of [Java: Date from unix timestamp](http://stackoverflow.com/questions/3371326/java-date-from-unix-timestamp) and [this](http://stackoverflow.com/q/454315/642706) and many many others. – Basil Bourque Aug 08 '14 at 19:24

1 Answers1

3

It sounds like your timestamp format is probably in seconds since the Unix epoch, instead of the milliseconds that Java expects - so just multiply it by 1000:

Date date = new Date(jsonObject.getLong("release_date") * 1000L);

You should also think about what time zone you're interested in, and set that on your SimpleDateFormat.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Update for [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html): `Instant instant = Instant.ofEpochSecond( 1_407_279_600L ) ;` `2014-08-05T23:00:00Z` – Basil Bourque Oct 12 '16 at 01:06