Get hour in current timezone from unix time
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());
Try this:
long l = Long.parseLong(oslist.get(position).get("hour")); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(l); calendar.setTimeInMillis(l * 1000); Date date = calendar.getTime(); //current date and time in UTC SimpleDateFormat df = new SimpleDateFormat("HH"); //HH = 0-23 hours df.setTimeZone(TimeZone.getDefault()); //format in given timezone String hourStringValue = df.format(date); int hourIntValue = Integer.parseInt(hourStringValue);