mmlooloo mmlooloo - 1 year ago 81
Java Question

Java timestamp, can not get correct value

I want to convert this

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?


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());
mIssue.getReleaseTime() = 1416990366

can anyone help?

Answer Source

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());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download