0

Say there's a component in the app that overrides the default time zone with TimeZone.setDefault(), and let's assume that's OK!
(I see it's a bad practice, but for the sake of this question, let's just go with it.)

In some places very limited cases I still need the actual device's time zone, but TimeZone.getDefault() will provide the overridden value.

Is there a way to query the device's actual time zone (set in Android settings/provided by network) without asking Java's TimeZone#getDefault?

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
  • Can you not just cache the original timezone provided to the JVM in which your app was run? – David Rawson Feb 15 '17 at 18:14
  • Have a look [here](http://stackoverflow.com/questions/8049912/how-can-i-get-the-network-time-from-the-automatic-setting-called-use-netw/14720918#14720918), this might be usefull – Atiq Feb 15 '17 at 18:39

1 Answers1

0

You could use internet, use this API request (from lat long):

http://api.geonames.org/timezoneJSON?formatted=true&lat=44&lng=11&username=demo

it returns:

 {
  "sunrise": "2017-02-15 07:15",
  "lng": 11,
  "countryCode": "IT",
  "gmtOffset": 1,
  "rawOffset": 1,
  "sunset": "2017-02-15 17:45",
  "timezoneId": "Europe/Rome",
  "dstOffset": 2,
  "countryName": "Italy",
  "time": "2017-02-15 19:27",
  "lat": 44
}
Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
  • That doesn't obey the user preference. If you go to settings, you can actually change your time zone to something else other than what's at your physical location. – TWiStErRob Feb 16 '17 at 18:43