The builtin Java classes don't offer this, but ICU's TimeZone class does, and TimeZone.getAvailableIDs("US") provides the correct answer.
The builtin Java classes don't offer this, but ICU's TimeZone class does, and TimeZone.getAvailableIDs("US") provides the correct answer.
As per the documentation the getTimeZone method returns the specified TimeZone, or the GMT zone if the given ID cannot be understood. There's no TimeZone ID called US hence it gives the GMT zone. If you really want to get all the list of TimeZones available in US, I would suggest you to use the following.
final List<String> timeZonesInUS = Stream.of(TimeZone.getAvailableIDs())
.filter(zoneId -> zoneId.startsWith("US")).collect(Collectors.toList());
A Locale is not a TimeZone, and vice versa. Check the Javadoc for the method you're using - the very first line says
Gets a calendar using the default time zone and specified locale.
That's why you're getting the default timezone - since you didn't specify one when obtaining a Calendar.
Think about what Jon said - if you know what timezone you would want to use in the situation where you've worked out a user is from the US, then you can call the Calendar.getInstance method that takes a timezone and a locale. On the other hand, if you can't say definitely what you would do here, then go back to the drawing board and think about your requirements a little more, rather than looking at your implementation.
If you can't answer the previous question, I think the standard recourse of most websites is to allow users to specify their preferred timezone (if they have a persistent account on the server) and default them to the server's timezone if they haven't said otherwise. If they don't have persistent accounts, and they're supplying information to you with times in (e.g. an XML upload), then they will have to either specify what time zone they're using in the request, or (probably better) you mandate the use of UTC for all times.
You can use ICU4J for this... See http://helpdesk.objects.com.au/java/can-i-find-all-available-timezones-for-a-country
If I understand your question, you could try something like this -
String[] tzIds = TimeZone.getAvailableIDs();
List<String> al = new ArrayList<String>();
for (String timeZoneId : tzIds) {
if (timeZoneId.startsWith("Canada")) {
al.add(timeZoneId);
}
}
System.out.println(al);
The output (slightly re-formatted) here is
[Canada/Pacific, Canada/Yukon, Canada/Mountain, Canada/Central,
Canada/East-Saskatchewan, Canada/Saskatchewan, Canada/Eastern,
Canada/Atlantic, Canada/Newfoundland]
You can do:
TimeZone tz = TimeZone.getTimeZone (id);
Where id has to be an Olson name of the form Area/Location, such as America/Los_Angeles.
You can get the available time zones using:
String[] ids = TimeZone.getAvailableIDs(0);
The official documentation: http://developer.android.com/reference/java/util/TimeZone.html
I think ICU4J package will help you.
You can shorten your list with hasSameRules()... this should reduce you selection to about 50:
iterate through -> file equal time zones -> choose the most recognizables
The country- list has to have about 200 entries with a whole lot of uninteresting ones such as Gibraltar or St Martin... don't like that idea