2

There is an excellent answer on how to get TimeZone given a Latitude and Longitude. There is however a comment in the answer as

One thing I've noticed is the lack of any UTC timestamp requirement when determining the time zone. For instance, a long/lat in London is not enough to determine weather the time zone is GMT or BST (British Summer Time / daylight savings). So surely to determine the correct time zone you need lat, long and a UTC timestamp.

Also, there is a wiki, which talks that Java has it's own database for Timezones, which is documented on Oracle website

I am wondering that given a UTC Datetime, Latitude and Longitude, is there a way to leverage the Java's timezone database to get the timezone instead of making API calls?

Any resources/examples?

Community
  • 1
  • 1
daydreamer
  • 87,243
  • 191
  • 450
  • 722
  • I don't see how. The timezone database contains rules telling what the UTC offsets and DST rules are for all timezones. But which point on the planet is governed by which timezone isn't in the database, AFAIK, and can change based on political decisions, etc. – JB Nizet Feb 27 '16 at 14:57
  • So the only way to get this information is to depend on external web services like the ones mentioned in question link? – daydreamer Feb 27 '16 at 14:58
  • No. I guess you could find (or create) a database of geolocalized timezones and embed it in your application. But just as the timezone database, it would have to be regularly updated to stay correct. Let's say for example that tomorrow, Syria or Iraq is split into three different countries. You can't tell which timezone(s) the governments of these 3 new countries will choose to adopt. – JB Nizet Feb 27 '16 at 15:02
  • 1
    Yeah, Russia changed its timezones at least twice in the last 7 years or so. Maybe more than twice. You'll never get it right by guessing. Moreover, you need a whole history of those changes because past rules still work for past timestamps. – Sergei Tachenov Feb 27 '16 at 15:06
  • @SergeyTachenov just by curiosity, did Russia change the UTC offset or DST rules of some of its timezones (i.e/ Europe/Moscow went from UTC+x to UTC+y), or did they change the timezone of some Russian cities/regions (i.e. city x was in the timezone Europe/Moscow and is now in the timezone Europe/Kaliningrad)? – JB Nizet Feb 27 '16 at 15:13
  • *"One thing I've noticed is the lack of any UTC timestamp requirement when determining the time zone. "* ... I can't make any sense of this comment. A UTC timestamp has nothing to do with a time -zone- (which is a particular geographical location with a historical set of rules for resolving local time). You don't need a UTC timestamp to get a time zone, but you do need a time zone in order to correctly offset a UTC timestamp into local time. – scottb Feb 27 '16 at 21:49
  • 1
    @JBNizet - Both have and will occurred. You can read about [2014's changes](http://www.timeanddate.com/news/time/russia-abandons-permanent-summer-time.html), and [the new changes at the end of March 2016](http://www.timeanddate.com/news/time/russia-astrakhan-time-zone.html). – Matt Johnson-Pint Feb 28 '16 at 17:17

1 Answers1

2

The article you referenced shows several libraries that work offline, including one for Java. These libraries look up lat/lon against the tz_world map, then give you back a standard IANA time zone identifier, such as America/New_York.

Then, you use that identifier with whatever timestamp to convert, using Java libraries or standard Java. The best approach is to use Joda-Time for Java 7 and earlier, and use the new java.time package for Java 8+.

There's not a one-step implementation, because these are two different concerns. You might need both in some cases, but not always.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575