I am using the following code to try to get the HOUR_OF_DAY (0-23) of a unix timestamp, converted to milliseconds. The timestamp '1296442971' converts to Sun Jan 30 2011 22:02:51 GMT-0500 (EST).
I'm running the following code to try to get the 24-hr timestamp:
//calculate the hour for this timestamp
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone(tz));
calendar.setTimeInMillis(ts * 1000);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int twelveHour = calendar.get(Calendar.HOUR);
In this example, both 'hour' and 'twelveHour' have the value 10, when 'hour' should have the value '22'. Does anyone have any ideas as to what could be wrong with my code?
Thanks!
Assuming ts is the variable containing the value 1296442971. I believe you have not declared it to be of type long and hence it might be overflowing
Below works after changing ts to long type
long l = 1296442971;
calendar.setTimeInMillis(l * 1000);
out.println(calendar.getTime());
out.println(calendar.get(Calendar.HOUR_OF_DAY));
out.println(calendar.get(Calendar.HOUR));