Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am getting the timezone of a android device using this code

TimeZone tz = TimeZone.getDefault(); String current_Time_Zone = (TimeZone.getTimeZone(tz.getID()).getDisplayName(                 false, TimeZone.SHORT)) 

But it always return me the timezone like "IST" but i want to get the timezone in GMT like this GMT+7:00.

like image 796
Naveen Kumar Avatar asked Feb 25 '13 13:02

Naveen Kumar


People also ask

How do I get GMT time zone?

If the specified string doesn't match the syntax, "GMT" is used. For example, TimeZone. getTimeZone("GMT-8"). getID() returns "GMT-08:00".

How do I find the offset of time zones?

You can get the offset of a timezone using ZonedDateTime#getOffset . Note that the offset of a timezone that observes DST changes as per the changes in DST. For other places (e.g. India), it remains fixed. Therefore, it is recommended to mention the moment when the offset of a timezone is shown.

How do I get time zone on my Android?

According to http://developer.android.com/reference/android/text/format/Time.html you should be using Time. getCurrentTimezone() to retrieve the current timezone of the device.


2 Answers

This might give you an idea on how to implement it to your liking:

Calendar mCalendar = new GregorianCalendar();   TimeZone mTimeZone = mCalendar.getTimeZone();   int mGMTOffset = mTimeZone.getRawOffset();   System.out.printf("GMT offset is %s hours", TimeUnit.HOURS.convert(mGMTOffset, TimeUnit.MILLISECONDS));  

(TimeUnit is "java.util.concurrent.TimeUnit")

like image 96
kaderud Avatar answered Oct 13 '22 03:10

kaderud


This code return me GMT offset.

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"),                 Locale.getDefault()); Date currentLocalTime = calendar.getTime(); DateFormat date = new SimpleDateFormat("Z"); String localTime = date.format(currentLocalTime); 

It returns the time zone offset like this: +0530

If we use SimpleDateFormat below

DateFormat date = new SimpleDateFormat("z",Locale.getDefault()); String localTime = date.format(currentLocalTime); 

It returns the time zone offset like this: GMT+05:30

like image 24
Naveen Kumar Avatar answered Oct 13 '22 03:10

Naveen Kumar