获取实时 - 在Android中不设置设置时间 [英] Get Real Time - Not Device Set Time in android

查看:102
本文介绍了获取实时 - 在Android中不设置设置时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找出真正的地理时间,就像我的应用程序一样,我想附加我的应用程序。

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 -

我不想要设备时间,我想要任何时候真正的预订时间

推荐答案

您需要使用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);
    }

}

这篇关于获取实时 - 在Android中不设置设置时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆