For a solution covering all thinkable city names this will require a database containing cities and their corresponding time zones. It will be further complicated by the fact that cities with the same name exist, so you may have ambiguous input. For example, by Paris I suppose you intended the capital and largest city of France, Europe, but towns called Paris exist in other places too. I don’t know if a suitable database exists, you may search.
I can get you close, though, with what is built into Java. Time zones have IDs in the form region/city, for example Australia/Sydney and Asia/Dubai. The city used in naming the time zone is the largest populated area of the time zone, so even in the case where a country or state is only one time zone, the city needs not be the capital. But if the city coincides, we can find the zone.
Set<String> zids = ZoneId.getAvailableZoneIds();
String[] cityNames = { "Abu Dhabi", "Dubai", "Sydney", "Dhaka", "Paris", "Indianapolis", "São Tomé" };
for (String cityName : cityNames) {
String tzCityName = Normalizer.normalize(cityName, Normalizer.Form.NFKD)
.replaceAll("[^\\p{ASCII}-_ ]", "")
.replace(' ', '_');
List<String> possibleTimeZones = zids.stream()
.filter(zid -> zid.endsWith("/" + tzCityName))
.collect(Collectors.toList());
System.out.format("%-12s %s%n", cityName, possibleTimeZones);
}
The output from this snippet is:
Abu Dhabi []
Dubai [Asia/Dubai]
Sydney [Australia/Sydney]
Dhaka [Asia/Dhaka]
Paris [Europe/Paris]
Indianapolis [America/Indianapolis, America/Indiana/Indianapolis]
São Tomé [Africa/Sao_Tome]
You will notice, though, that it didn’t find any time zone for Abu Dhabi because although the capital of the United Arab Emirates, it is not the largest city; Dubai is. You will notice too that two time zones were found for Indianapolis. The former is just an alias for the latter, though.
The city names used in the time zone database are the English names (when they exist) stripped of any accents. When a name is in two or three words, they are separated by underscores rather than spaces. So São Tomé becomes Sao_Tome. Therefore in the code I am performing this conversion. The way to strip off the accents was taken from another Stack Overflow answer, link below.
Links
- List of tz database time zones on Wikipedia
- Answer by Erick Robertson to Is there a way to get rid of accents and convert a whole string to regular letters?
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());
This depends on information the "location" contains? You'd somehow need to map the location to a timezone name, preferably the Olson style timezone names, because they are more detailed and easier to map, as they are locations themselves.
If it's an approximate addres (like country and city or so) then several geolocation services do include timezones in their information, so you can call these services and see.
If it's a geolocation with latitude and longitude then a site called Earthtools can give you the timezone. http://www.earthtools.org/webservices.htm#timezone
There is this database that provides mappings from cities and countries to timezones: http://citytimezones.info/cms/pending_requests.htm
Unfortunately it uses Windows timezone names, but you can this data http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml from Unicode.org to map between Windows timezone names and the Olson TZ names.
I haven't actually done this but the following should work:
You can first use GeoGoogle java library to get the longitude/latitude from the city-state-country.
Next, you can use EarthTools (and some java code of your own) mentioned by Lennart to get the timezone :)
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
java.util.TimeZone tz = java.util.TimeZone.getTimeZone("GMT+1");
java.util.Calendar c = java.util.Calendar.getInstance(tz);
System.out.println(c.get(java.util.Calendar.HOUR_OF_DAY)+":"+c.get(java.util.Calendar.MINUTE)+":"+c.get(java.util.Calendar.SECOND));
I'd encourage you to check out Joda Time, an alternative (but very popular) to the standard Java date and time API:
http://joda-time.sourceforge.net/index.html
Using Joda Time, I think this is what you what:
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
public class TimeZoneDemo {
public static void main(String[] args) {
DateTime now = new DateTime(System.currentTimeMillis(), DateTimeZone.forID("UTC"));
System.out.println("Current time is: " + now);
}
}
You just need to know the standard ID for the time zone in question, such as UTC.