0

I want to use the current online time in my Android application. I know that you can make sure that the time is provided by the Internet in the Settings of Android. How can I check that in Java?

Tunaki
  • 132,869
  • 46
  • 340
  • 423

2 Answers2

0

You would need to check it yourself, using an SNTP client or something.

There is no requirement that a device use "the Internet" for automatically setting the time, even if the user has not manually changed the time. For example, many devices in the US rely on NITZ signals from the wireless carriers as their time source.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

It seems that there's already a similar question here: How to use an Internet time server to get the time?

I must admit I haven't personaly used this, but it looks like it's worth a try. It uses apache commons library.

Copying relevant code (full explanation on the link):

String TIME_SERVER = "time-a.nist.gov";   
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
Date time = new Date(returnTime);

Something it doesn't say there is that the TIME_SERVER may not be updated. Here's a list of servers with their availability: http://tf.nist.gov/tf-cgi/servers.cgi#

Please let me know if this worked for you :)

Community
  • 1
  • 1
gnardini
  • 73
  • 6