0

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • https://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java/6697884#6697884 see this. – Prashant Sable Dec 03 '19 at 10:31
  • 1
    @PrashantSable It will return time from device. – Karan Khurana Dec 03 '19 at 10:32
  • If you have network access then get it from network: https://stackoverflow.com/questions/31129835/how-to-get-time-from-network-in-android – Md. Asaduzzaman Dec 03 '19 at 10:34
  • Does this answer your question? [How to get time from Network in Android](https://stackoverflow.com/questions/31129835/how-to-get-time-from-network-in-android) – Ole V.V. Dec 04 '19 at 09:18

2 Answers2

1

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.

Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
0

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

ndelanou
  • 523
  • 5
  • 16