I've a problem creating new date in Java. I receive from webservice a timestamp (for example 1397692800). You can see from this online converter that is equal to 17 Apr 2014. When I try to create a new date with this timestamp in Java
Date date = new Date (1397692800);
console says to me that is equal to Sat Jan 17 05:14:52 CET 1970. What's wrong with my code?
The Date(long)
constructor takes the number of milliseconds since the Unix epoch. I strongly suspect that you've been given the number of seconds since the Unix epoch - so just multiply that value by 1000L.
Date date = new Date(timestamp * 1000L);
Make sure you use 1000L rather than 1000, as otherwise if timestamp
is an int
, it will perform the arithmetic in 32 bits instead of 64, and you're very likely to get the wrong results.
See more on this question at Stackoverflow