Im trying to convert unixtime to date but the results im getting are wrong :
for example i have this unixtime : 1354312800 accurding to this site :
enter link description here
the result is :
Fri, 30 Nov 2012 22:00:00 GMT
but when i do :
long timestamp = 1354312800;
java.util.Date time=new java.util.Date((long)timestamp*1000);
int d = time.getDay();
int m = time.getMonth();
im getting :
d= 6 << this is wrong should be 30.
and m – 11
The method
Date.getDay()gives the day of the week (0 = Sunday, …, 6 = Saturday).Change it to
Date.getDate()and you will get30as the result.Some side-notes:
The
Dateclass is pretty much deprecated. UseCalendarinstead, or even better, the Joda time library.Your conversion is sort of funny.
(long)timestamp*1000convertstimestampto a long value (which is already a long value) and then automatically widens1000to a long value to carry out the multiplication.I would skip the conversion,
(long), altogether, and if you want to explicitly say that the factors are long values, use1000Linstead, which is a long-literal.