How to get TimeZone from android mobile?

JavaAndroidTimezone

Java Problem Overview


I want to get the time zone from the Android mobile when clicking a button.

Java Solutions


Solution 1 - Java

Have you tried to use TimeZone.getDefault():

> Most applications will use TimeZone.getDefault() which returns a TimeZone based > on the time zone where the program is running.

Ref: http://developer.android.com/reference/java/util/TimeZone.html

Solution 2 - Java

TimeZone tz = TimeZone.getDefault();
System.out.println("TimeZone   "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezone id :: " +tz.getID());

Output:

> TimeZone GMT+09:30 Timezone id :: Australia/Darwin

Solution 3 - Java

Edit: corrected the case

TimeZone.getDefault()

Solution 4 - Java

I needed the offset that not only included day light savings time but as a numerial. Here is the code that I used in case someone is looking for an example.

I get a response of "3.5" (3:30') which is what I would expect in Tehran , Iran in winter and "4.5" (4:30') for summer .

I also needed it as a string so I could post it to a server so you may not need the last line.

for getting currect time zone :

TimeZone tz = TimeZone.getDefault();
Date now = new Date();
//Import part : x.0 for double number
double offsetFromUtc = tz.getOffset(now.getTime()) / 3600000.0;
String m2tTimeZoneIs = Double.parseDouble(offsetFromUtc);

Solution 5 - Java

Try this code-

Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();

It will return user selected timezone.

Solution 6 - Java

ZoneId from java.time and ThreeTenABP

Modern answer:

	ZoneId zone = ZoneId.systemDefault();
	System.out.println(zone);

When I ran this snippet in Australia/Sydney time zone, the output was exactly that:

> Australia/Sydney

If you want the summer time (DST) aware time zone name or abbreviation:

	DateTimeFormatter longTimeZoneFormatter = DateTimeFormatter.ofPattern("zzzz", Locale.getDefault());
	String longTz = ZonedDateTime.now(zone).format(longTimeZoneFormatter);
	System.out.println(longTz);
	
	DateTimeFormatter shortTimeZoneFormatter = DateTimeFormatter.ofPattern("zzz", Locale.getDefault());
	String shortTz = ZonedDateTime.now(zone).format(shortTimeZoneFormatter);
	System.out.println(shortTz);

> Eastern Summer Time (New South Wales) > EST

The TimeZone class used in most of the other answers was what we had when the question was asked in 2011, even though it was poorly designed. Today it’s long outdated, and I recommend that instead we use java.time, the modern Java date and time API that came out in 2014.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • Edit: On (older) Android usually, as long as you're on Android Gradle plugin 4.0 or newer, with coreLibraryDesugaring you can use java.time directly. ThreeTenABP is no longer needed. (Previous bullet: use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.)

Solution 7 - Java

TimeZone timeZone = TimeZone.getDefault(); 
timeZone.getID();

It will print like

Asia/Kolkata

Solution 8 - Java

Simplest Solution With Simple Date Format: SimpleDateFormat("ZZZZZ"):

 Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
                Locale.getDefault());
        Date currentLocalTime = calendar.getTime();

        DateFormat date = new SimpleDateFormat("ZZZZZ",Locale.getDefault());
        String localTime = date.format(currentLocalTime);
        System.out.println(localTime+ "  TimeZone   " );

==> Output is : +05:30

Solution 9 - Java

On my device TimeZone.getDefault() is always returning UTC time zone.

I need to do this to get user configured time zone :

TimeZone.setDefault(null)
val tz = TimeZone.getDefault()

It will return user selected timezone.

Solution 10 - Java

All the answers here seem to suggest setting the daylite parameter to false. This is incorrect for many timezones which change abbreviated names depending on the time of the year (e.g. EST vs EDT).

The solution below will give you the correct abbreviation according to the current date for the timezone.

val tz = TimeZone.getDefault()
val isDaylite = tz.inDaylightTime(Date())
val timezone = tz.getDisplayName(isDaylite, TimeZone.SHORT)

Solution 11 - Java

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.

Solution 12 - Java

For devices with API 26 and higher, you can get it like this:

ZonedDateTime.now().getZone().toString();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionasha vView Question on Stackoverflow
Solution 1 - JavaMasterCassimView Answer on Stackoverflow
Solution 2 - JavaZulfiqarView Answer on Stackoverflow
Solution 3 - JavajmjView Answer on Stackoverflow
Solution 4 - JavaAdnan Abdollah ZakiView Answer on Stackoverflow
Solution 5 - JavaanujprasharView Answer on Stackoverflow
Solution 6 - JavaOle V.V.View Answer on Stackoverflow
Solution 7 - JavaRaja JawaharView Answer on Stackoverflow
Solution 8 - JavaJaydeep purohitView Answer on Stackoverflow
Solution 9 - JavaBipiView Answer on Stackoverflow
Solution 10 - JavaDummyDataView Answer on Stackoverflow
Solution 11 - JavaThomView Answer on Stackoverflow
Solution 12 - JavaMaciej BeimcikView Answer on Stackoverflow