Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get the Name of TimeZone in Android

I would like to show the "name" of timezone in my android program. Say if it is "GMT+8:00", then show "HongKong"; By searching, i find that the getDisplayName function should sever my purpose.

http://developer.android.com/reference/java/util/TimeZone.html#getDisplayName(boolean, int, java.util.Locale)

However, in my own program, this function only shows "GMT+8:00" but when i use "getDisplayName" in the Google Open Source Project, it would show the name "HongKong" instead.

Does anyone know the reason behind this?

like image 676
Antoine Murion Avatar asked Dec 15 '14 08:12

Antoine Murion


People also ask

What is timezone getDefault ()?

The getDefault() method of TimeZone class in Java is used to know the default TimeZone for this system or host. This may vary in according to the implementation in different environment. Syntax: public static TimeZone getDefault() Parameters: The method does not take any parameters.


2 Answers

Try this

TimeZone tz = TimeZone.getDefault();
System.out.println(tz.getID());

getDisplayName returns timezone and getId returns timezone location name.

like image 26
Praveena Avatar answered Sep 27 '22 21:09

Praveena


You can find out your current time zone by below code

private String getTimeZone() {
        Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
        TimeZone zone = calendar.getTimeZone();
        return zone.getID();
}
like image 109
Mihir Patel Avatar answered Sep 27 '22 22:09

Mihir Patel