Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How to find out the Real Geographical Time, as in my application I want to attach the real time with my application.

So that if the user changes the time in his device then I should get only the real prevailing time - NOT THE DEVICE TIME

My question is much confusing but try to understand in a simple language -

I dont want DEVICE TIME, I want REAL PREVAILING TIME at any instant

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
794 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to use the NTP (Network Time Protocol) protocol:

Here is some code I found somewhere else... and I am using it. This uses the Apache Commons library, which can be installed using Gradle (adding a dependency on commons-net:commons-net:3.6)

If you need a list of time servers, check: http://tf.nist.gov/service/time-servers.html

Here is some Java Code for you to use:

public class TimeLookup {
    public static final String TIME_SERVER = "time-a.nist.gov";

    public static void main(String[] args) throws Exception {
        NTPUDPClient timeClient = new NTPUDPClient();
        InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
        TimeInfo timeInfo = timeClient.getTime(inetAddress);
        long returnTime = timeInfo.getReturnTime();
        Date time = new Date(returnTime);
        System.out.println("Time from " + TIME_SERVER + ": " + time);
    }

}

Note the Android code must run on the background thread and include gradle dependency:

implementation 'commons-net:commons-net:3.6'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...