Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert epoch time to date and time in Java?

I need to convert epoch timestamp to date and time. I have used the following code to convert but it converts to a wrong date, year and time.

String date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")
                    .format(new java.util.Date (1319286637/1000));

Expected output was today’s date at some time, but the result I got was:

01/01/1970 05:51:59

like image 959
arnp Avatar asked Oct 22 '11 07:10

arnp


1 Answers

The Date(long) constructor takes milliseconds. You should be multiplying by 1000, not dividing the epoch time you have.

like image 195
Mat Avatar answered Oct 12 '22 19:10

Mat