1

In my Android app, I would like to retrieve the current date/time (UTC) from some server. Android does have the built-in ability to synchronize the device's date and time with some server but I have found this to be unreliable. Does Google or some other well known service provider provide a simple API that would allow me to retrieve the current date/time? I am not interested in trying to set the date/time on the device (that's not even possible). But I do need to know the correct date in order to perform scheduled tasks that depend on exact dates.

I have seen a device reset its date to something like 1980 when the battery has been removed for an extended period of time and therefore I cannot rely upon the date of the device.

EDIT:

I came across this post: https://stackoverflow.com/a/13066268/753632

But it requires installing Apache Commons Net. Kind of overkill just to get the time. I don't need to bloat my code more than it already is.

Community
  • 1
  • 1
Johann
  • 27,536
  • 39
  • 165
  • 279

2 Answers2

0

you can refer this link How can I get the "network" time, (from the "Automatic" setting called "Use network-provided values"), NOT the time on the phone?

Community
  • 1
  • 1
Hardik
  • 94
  • 1
  • 1
  • 11
-2

You could use the calender class, like is explained here, although this uses the time of the device itself. You could also read this it explains how to get the time for the service provider.

I think using the calender and then calculating the UTC time with based on the timezone the phone is in (like this). Because the phone already gets it's time from a server when it is connected to the internet.

I hope this helps.

EDIT: I see you edited your post. So you don't want to use the device time... I think you could look into getting it from the service provider then.

Community
  • 1
  • 1
WereWolfBoy
  • 498
  • 1
  • 4
  • 23