Is it possible to list available TimeZones by Locale in Java?
I need to generate a list of timezones to display for a user. We'd like to narrow the list down depending 开发者_开发知识库on the user's Locale.
If the user has a locale of 'en_US', he probably doesn't care to see 'Asia/Ashkhabad' in the list.
Is this doable?
No you can't. You can use TimeZone.getAvailableIDs(int rawoffset) to narrow the results, but then you need to keep a map of Locale->raw offsets.
Also, what happens if your user is an American located in India? Don't you want to display the Asia/Kolkata
time zone for him?
Of course it's doable... but you'll have to write some code.
You'll have to front your time zone selection with code which filters the list of timezones. You'd populate that filter with a map of locales to the set of timezones for the locale.
However, as David noted, you will face the issue of people whose Locale is not their work location or timezone, so be prepared to deal with this in a two-tiered approach, or to pick up "virtual locale" from somewhere.
You might find THIS useful in implementing what you want to do.
精彩评论