Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion from Unix Time to Java Calendar

The following is driving me crazy, why am I getting the wrong calendar date when converting Unix Time 1386230874 using the code below!!

This should be Thu Dec 5 19:07:54 2013

Output:

Comment posted on:Sat Jan 17 11:03:50 EST 1970

Code:

Calendar facebook_created_time_calendar = Calendar.getInstance(TimeZone.getTimeZone("Australia/Sydney"));    
facebook_created_time_calendar.setTimeInMillis(1386230874);
out.print("Comment posted on:");
out.println(facebook_created_time_calendar.getTime());
like image 512
Ossama Avatar asked Dec 05 '13 08:12

Ossama


1 Answers

Java works with millisecond timestamps, while Unix time stamps are typically measured in seconds. Multiply the Unix timestamp by 1000L to get the right time.

like image 88
Joni Avatar answered Sep 25 '22 07:09

Joni