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.
If the specified string doesn't match the syntax, "GMT" is used. For example, TimeZone. getTimeZone("GMT-8"). getID() returns "GMT-08:00".
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.
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.
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")
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With