views:

171

answers:

2

Hi
I need to convert unix timestamp to date.
I did it like this:

java.util.Date time=new java.util.Date(timeStamp);

timestamp value is: 1280512800 and time must be "2010/07/30 - 22:30:00" (as I get it by PHP) but it is "Thu Jan 15 23:11:56 IRST 1970" by the code above!

What's the problem?

+6  A: 

Multiply by 1000, since java is expecting milliseconds:

java.util.Date time=new java.util.Date(timeStamp*1000);

From the documentation:

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

Pablo Santa Cruz
+2  A: 

Date's constructor expects the timeStamp value to be in milliseconds. Multiply your timestamp's value with 1000, then pass is to the constructor.

f1sh