-4

I've seen

Convert from UNIX Timestamp to "Today at: currentTime" using Java

Convert unix time stamp to date in java

but none of these is working for me.

I have a timestamp : 1503037706145

And I'm using the following code to convert it to a readable date:

Long leadTime = leadData.getLong("addedOn");

Date date = new Date(leadTime*1000);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy  |  hh:mm a");
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
String formattedDate = sdf.format(date);

It's returning 15/05/49599 11:25:45PM

But when I check it on this, it returns Friday, 18 August 2017 07:00:14.817

which is correct. What am I doing wrong ??

Community
  • 1
  • 1
mrid
  • 5,782
  • 5
  • 28
  • 71
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 19 '17 at 01:20
  • 1
    thanks @BasilBourque , I didn't know that. I'll see the tutorial :) – mrid Aug 19 '17 at 05:13

3 Answers3

2

Other Answers are correct about not needing to multiply your count-from-epoch by a thousand as you already have a number in milliseconds.

Avoid legacy date-time classes

But the other Answers and the Question all use troublesome classes that are now legacy, supplanted by the java.time classes. See Oracle Tutorial.

java.time

For Android, see the ThreeTen-Backport and ThreeTenABP projects.

Instant is a moment on timeline in UTC resolved to nanoseconds.

Instant instant = Instant.ofEpochMilli( 1503037706145 ) ;  // Instant is a moment on timeline in UTC resolved to nanoseconds.
String output = instant.toString() ;  // Generate string in standard ISO 8601 format. 

Assign a time zone if desired.

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

The timestamp you currently have is OK for Android. The fact that you need to multiply it by 1000 to make it work on www.epochconverter.com, indicates that epochconverter uses a different format. Just throw leadTime in Date and you should be fine.

0xDEADC0DE
  • 2,453
  • 1
  • 17
  • 22
  • 1
    Timestamps are in milliseconds and OP's stamp is in milliseconds. The conclusion of *1000 not required is correct anyway. – laalto Aug 18 '17 at 10:02
  • @laalto. OK, I'll update that. I always thought that it were seconds. I thought epochconverter.com (the linked site) used milliseconds – 0xDEADC0DE Aug 18 '17 at 10:05
  • and @laalto, how do you figure out whether ts is in seconds or milliseconds ? – mrid Aug 18 '17 at 10:06
  • @0xDEADC0DE you're right. OP's timestamp is in millis so the *1000 is not needed. 1503037706 is the seconds value. Removing the *1000 is the answer. – BennyLava Aug 18 '17 at 10:06
  • 1
    The timestamp has 3 more digits than a seconds-based timestamp would have. epochconverter can guess that such timestamps are in milliseconds, even if normally it would use seconds as input. – laalto Aug 18 '17 at 10:07
  • When you put OP's timestamp into Epoch converter it shows this message "Assuming that this timestamp is in milliseconds". The converter knows that a millisecond value was input and is converting it to seconds. – BennyLava Aug 18 '17 at 10:08
1

I suggest using DateFormat.format("MMM dd, hh:mm a", timeInMillisec * 1000) as some versions of android won't support SimpleDateFormat.

james
  • 21
  • 3