且构网

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

获取实时 - 在Android中不设置设置时间

更新时间:2023-02-26 17:26:01

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

是我找到一个别的东西的代码..我正在使用它。这使用apache commons库。

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代码

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

}