Reputation: 11
I am trying to get actual time in my android application instead of getting what the system/device returned.
Suppose today is date 3 dec and I have changed my system date to 4 dec, now I will get the date 4 dec in my application which is not correct.
I have applied following code:
val c = Calendar.getInstance()
c.timeZone = TimeZone.getTimeZone("UTC")
val hour = c.get(Calendar.HOUR_OF_DAY)
val minute = c.get(Calendar.MINUTE)
val sec = c.get(Calendar.SECOND)
val mYear1 = c.get(Calendar.YEAR)
val mMonth1 = c.get(Calendar.MONTH)
val mDay1 = c.get(Calendar.DAY_OF_MONTH)
to get the time. But this only returned the device/system date.
All I want is to get the actual date. (i.e.. 3 dec in my case not 4 dec).
I also tried to get the time from GPS, but this only returns the time from the last known location, which can get me trouble.
Upvotes: 0
Views: 728
Reputation: 921
You will have to use a NTP (Network Time Protocol) server if you can't trust your user device time
I found this repository: https://github.com/AllanHasegawa/Tempo
Upvotes: 0
Reputation: 6961
If you don't trust user settings or device time, you can simply use an API, such as http://worldtimeapi.org/ and get the current time from it in any time zone you like.
Upvotes: 1