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.
Output : GMT+5:30
Generally you cannot translate from a time zone like Asia/Kolkata to a GMT offset like +05:30 or +07:00. A time zone, as the name says, is a place on earth and comprises the historic, present and known future UTC offsets used by the people in that place (for now we can regard GMT and UTC as synonyms, strictly speaking they are not). For example, Asia/Kolkata has been at offset +05:30 since 1945. During periods between 1941 and 1945 it was at +06:30 and before that time at +05:53:20 (yes, with seconds precision). Many other time zones have summer time (daylight saving time, DST) and change their offset twice a year.
Given a point in time, we can make the translation for that particular point in time, though. I should like to provide the modern way of doing that.
java.time and ThreeTenABP
Output when running just now was:
For the default time zone set
zone
toZoneId.systemDefault()
.To format the offset with the text
GMT
use a formatter withOOOO
(four uppercase letter O) in the pattern:I am recommending and in my code I am using java.time, the modern Java date and time API. The
TimeZone
,Calendar
,Date
,SimpleDateFormat
andDateFormat
classes used in many of the other answers are poorly designed and now long outdated, so my suggestion is to avoid all of them.Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
org.threeten.bp
with subpackages.Links
java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).Use this code (Opt 1):
If I'm in Los Angeles (GTM-07:00 Pacific Standard Time) the output is:
Adding dst offset will solve this:
This might give you an idea on how to implement it to your liking:
(TimeUnit is "java.util.concurrent.TimeUnit")