I am trying to retrieve a converted "hour" integer for a user's time zone with GMT unix time. My code works SOME of the time, though for example, it was 9:00pm on the east coast and getting a 0 for the hour. can anyone help?
long l = Long.parseLong(oslist.get(position).get("hour"));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(l);
calendar.setTimeInMillis(l * 1000);
calendar.setTimeZone(TimeZone.getDefault());
int hour = calendar.get(Calendar.HOUR);
Log.v("TIME:", ""+hour);
You don't need to set the time zone - it's default by default, as it were. And calling setTimeInMillis
twice is pointless. So just:
Calendar calendar = calendar.getInstance();
calendar.setTimeInMillis(unixTimestamp * 1000L);
int hour = calendar.get(Calendar.HOUR);
... should be absolutely fine. If it's not, then going via string representations as suggested by other answers isn't going to help.
If that's giving 0 when it's 9pm on the East Coast, that suggests the default time zone is not one representing the East Coast. I suggest you diagnose that first:
System.out.println(TimeZone.getDefault().getID());
// Just in case the ID is misleading, what's the standard offset for this zone?
System.out.println(TimeZone.getDefault().getRawOffset());
See more on this question at Stackoverflow