Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UTC offset in seconds in android

Tags:

I'm trying to get the UTC offset in seconds in android. Since the emulator keeps returning 0, I can't tell if I'm doing this correctly or not.

    Calendar c=Calendar.getInstance();         TimeZone tz=c.getTimeZone();         int offsetFromUtc=tz.getOffset(0)/1000; 

When I put 0 in the tz.getOffset it means I just want the number of seconds from gmt. Let me know if what Im doing is correct. Thanks

like image 804
Sean Pan Avatar asked Sep 02 '11 22:09

Sean Pan


People also ask

How do you calculate UTC offset?

(GMT-5:00) Eastern Time (US & Canada)Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.

How do I find offset time zone?

Java TimeZone getOffset() Method The getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds) method of TimeZone class is used to get the offset of time zone, and the current date. This offset is used to get the local time.

How do I set UTC time zone on Android?

On a current Android device, tap the Clock app, tap the Globe icon (bottom of the screen), then search for UTC and tap the UTC result. On a current iOS device, tap the Clock app, tap World Clock, then + (in the upper-right corner), search for UTC, then tap the UTC result.

Does UTC have an offset?

A UTC offset is the difference in hours and minutes between a particular time zone and UTC, the time at zero degrees longitude. For example, New York is UTC-05:00, which means it is five hours behind London, which is UTC±00:00.


1 Answers

Time Zone offsets can change during the year, perhaps the most common reason is Daylight Savings Time. Instead of passing 0 to TimeZone.getOffset() uses the current time to get the current offset, create a new Date and get its Time value:

TimeZone tz = TimeZone.getDefault(); Date now = new Date(); int offsetFromUtc = tz.getOffset(now.getTime()) / 1000; 

The emulator defaults to UTC, try checking the available timezones to test. Also you can pass a timezone switch to set the timezone, check the documentation for -timezone.

like image 103
Dan S Avatar answered Sep 28 '22 02:09

Dan S