Reputation: 6437
Possisble duplicate:
Get Data from the internet android
Hi all, I'm developing an app, it can get real time at current location. My idea is get from internet, but i don't know how to got it. I had latitude and longitude from current location.
My questions:
Upvotes: 1
Views: 2489
Reputation: 1297
Now you can get time for the current location but for this you have to set the system's persistent default time zone.setTimeZone(String timeZone)
which can be get from
Calendar calendar = Calendar.getInstance();
long now = calendar.getTimeInMillis();
TimeZone current = calendar.getTimeZone();
setAutoTimeEnabled(boolean enabled)
Sets whether or not wall clock time should sync with automatic time updates from NTP.
TimeManager timeManager = TimeManager.getInstance();
// Use 24-hour time
timeManager.setTimeFormat(TimeManager.FORMAT_24);
// Set clock time to noon
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 12);
long timeStamp = calendar.getTimeInMillis();
timeManager.setTime(timeStamp);
I was looking for that type of answer I read your answer but didn't satisfied and it was bit old. I found the new solution and share it. :)
For more information visit: https://developer.android.com/things/reference/com/google/android/things/device/TimeManager.html
Upvotes: 0
Reputation: 11310
Try this. I get it from somewhere
I think it is not possible directly from android SDK. You can get a
timezone from a city name, but not from lat/long values (see
https://developer.android.com/reference/java/util/TimeZone.html
setID() and getAvailableIDs()
)
You can make a request to a webservice that will give you the time zone from lat/long. See http://stackoverflow.com/questions/55901/web-service-current-time-zone-for-a-city for more information.
Upvotes: 1