Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java convert unix timestamp to wrong time

I have unix timestammp stored in mysql. I am converting it into time. It displays wrong time.

Here is 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 variable which contains unix timestamp.
Ex: for timestamp=1417437428505 it should show 6:07 PM and showing 12:31 AM

What is solution for it?

like image 769
Devz Avatar asked Aug 12 '26 19:08

Devz


1 Answers

You're multiplying a timestamp which is already in milliseconds since the Unix epoch by 1000. You just want:

Date date = new Date(timestamp);

If you look at all of the date, not just the time, you'll see it's currently in 46886!

like image 92
Jon Skeet Avatar answered Aug 15 '26 09:08

Jon Skeet