I am doing an android app which has to display the current server/network operator time since the device time can be changed by the user. I don't want to change/set the device time. I tried few source codes but they didn't work well. I'm able to get device date and time but I need date and time from server or network provider itself. please help me
2 Answers
It seems that you need to interface with the Network Time Protocol (NTP).
There are similar questions here on SO use of ntp service, get datetime from network provider (as noted by Gheo) suggesting to:
- use
android.net.SntpClient.java
source code and javadocs - read through this Google Groups thread
As noted in the comments of these posts, as of Honeycomb you cannot do any network operations in the main activity thread (suggestion: use a service instead).
Update:
A further search led me to find Dominik Schürmann's ntp-sync repository on GitHub. He provides different examples of how you can use his library which you can compile and run on your emulator/device.
-
-
am new to android. i checked out sntpclient code and implemented it in my app also along with other codes. but no success – athira09 Feb 07 '14 at 09:45
-
My answer has as update... it's written in bold! I think it's a good answer to your question. If you have another question ask another question, Stackoverflow is a question&answer site, not a forum or a chat room – Oerd Feb 09 '14 at 20:45
-
I had found a solution but forgot to post it. In case somebody finds it helpful, here is what worked for me.
Get the server time using an API or network call wherein the response will have the timestamp from the server. Use the data as per your convenience.
You can modify the existing API itself, if any.

- 155
- 1
- 3
- 18