1

I want to convert this 1416990366 timestamp to date. here is how I do:

    Timestamp stamp = new Timestamp(1416990366);
    System.out.println("TimeStamp: " + stamp.toString());
    Date mDate = new Date(stamp.getTime());

but I get Sat Jan 17 13:06:30 GMT+03:30 1970 while it must be Wed, 26 Nov 2014 08:26:06 GMT what is wrong?

Edit:

I get this value from server and I think they cut off miliseconds so i have to multiply it by 1000 but I get nothing, and again the wrong value. why this still dose not work Timestamp stamp = new Timestamp(1000 * mIssue.getReleaseTime()); where mIssue.getReleaseTime() = 1416990366 can anyone help?

mmlooloo
  • 18,937
  • 5
  • 45
  • 64

2 Answers2

2

The time specified to the Timestamp constructor is the time in milliseconds since January 1, 1970, 00:00:00 GMT. 1416990366 evaluates to around 16 days from that epoch, hence the output that you're getting.

If you want the current time, you can pass System.currentTimeMillis() to the constructor.

EDIT: Since the time that is obtained from the server is in milliseconds, you can multiply by 1000 and convert to long as follows:

Timestamp stamp = new Timestamp((long) 1000 * mIssue.getReleaseTime());
M A
  • 71,713
  • 13
  • 134
  • 174
  • Thanks for helping, I get this value from server and I think they cut off miliseconds so i have to multiply it by 1000 but I get nothing, and again the wrong value. – mmlooloo Nov 26 '14 at 12:05
  • why this still dose not work `Timestamp stamp = new Timestamp(1000 * mIssue.getReleaseTime());` where `mIssue.getReleaseTime() = 1416990366` – mmlooloo Nov 26 '14 at 12:08
  • If this solves anything `System.out.println(new Date(0));` prints Thu Jan 01 **02:00:00** EET 1970! – Eypros Nov 26 '14 at 12:08
  • @mmlooloo You would need to convert the result to `long`. Answer updated. – M A Nov 26 '14 at 12:10
1

java.time

The modern way is with java.time classes.

Instant instant = Instant.ofEpochSecond( 1416990366L );

Call toString to generate a String that represents its date-time value in standard ISO 8601 format.

2014-11-26T08:26:06Z

An Instant is a moment on the timeline in UTC with a resolution of nanoseconds.

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

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    @mmlooloo See the last paragraph I added for ThreeTenABP project. – Basil Bourque Aug 23 '16 at 14:55
  • Thank you for introducing new library but actually in my case adding a new library also adds method counts and increases binary size of my apk, so still I prefer use old coding style unless I need a heavy time APIs . Again thanks :-) – mmlooloo Aug 23 '16 at 16:51