Java converting unix timestamp to wrong time

I have a unix timestammp stored in mysql. I will transform it in time. It displays the wrong time.

Here is the code:

Date date = new Date((long)timestamp*1000); 
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a"); 
sdf.setTimeZone(TimeZone.getTimeZone("GMT+5:30"));
timeString = sdf.format(date);
System.out.println(timeString);`

      

timestamp

is a variable that contains the unix timestamp.
Example: for timestamp=1417437428505

it should show 6:07 pm and show 12:31 am

What is this solution?

+3


source to share


3 answers


You are multiplying the timestamp, which is already in milliseconds since the Unix epoch, by 1000. You just want:

Date date = new Date(timestamp);

      



If you look at the entire date and not the time, you will see it currently at 46886!

+4


source


Are you sure you need to multiply by 1000? I tried to pass without multiplication Date date = new Date(timestamp);

and it printed 6:07 pm



+1


source


Remove multiply from 1000 to

Date date = new date ((long) timestamp * 1000);

how it works.

0


source







All Articles