且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取实时 - 而不是设备设置时间的android

更新时间:2023-02-26 17:51:48

您需要使用NTP(网络时间协议)协议:

下面是一个codeI发现somewhr别人..和我使用它。这种使用Apache公共资源库。

Here is a code i found somewhr else.. and i am using it. this uses apache commons library.

//名单的时间服务器:http://tf.nist.gov/service/time-servers.html

// List of time servers: http://tf.nist.gov/service/time-servers.html

下面是Java的code为您

Here is Java Code for you

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);
    }

}