Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the timezone offset in android?

I found some similar questions, such as:

How to get the timezone offset in GMT(Like GMT+7:00) from android device?

How to find out GMT offset value in android

But all these answers(+12:00) are incorrect for New Zealand Daylight Saving Time now.

When I did debug, I got this from Google Calendar event object:

"dateTime" -> "2016-11-06T10:00:00.000+13:00"

So how to get the correct offset which should be +13:00?

Thanks.

like image 669
J.W Avatar asked Nov 01 '16 06:11

J.W


People also ask

How do I get a time zone offset?

The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.

How do I get time zone on my Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken textview to show current time zone.

Does timezone offset change?

Breaking Down the Difference An offset is the number of hours or minutes a certain time zone is ahead of or behind GMT**. A time zone's offset can change throughout the year because of Daylight Saving Time. Sometimes laws change a time zone's offset or daylight savings pattern.

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.


1 Answers

To get the current offset from UTC in milliseconds (which can vary according to DST):

return TimeZone.getDefault().getOffset(System.currentTimeMillis());

To get a RFC 822 timezone String instead, you can simply create a SimpleDateFormat instance:

return new SimpleDateFormat("Z").format(new Date());

The format is (+/-)HHMM

like image 61
BladeCoder Avatar answered Sep 29 '22 13:09

BladeCoder