Android:从NTP服务器获取日期和时间 [英] Android: Getting the date and time from a NTP server

查看:981
本文介绍了Android:从NTP服务器获取日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我自己的时间服务器(即192.168.1.X)获取在我的Android应用程序中使用的日期和时间



我已经搜索在网络中有两种解决方案:



solution 1





但是,我不知道有没有导入任何图书馆的解决方案?



谢谢!!

解决方案

我最终使用这个



创建另一个类,然后在其中添加代码。然后从我的主要活动中调用该类来获取日期。



该代码有点旧,我通过添加AsyncTask ...对我来说修改正常。另外还有一件事,类的使用(从什么代码sugguest)给出String.valueof(现在)返回0.日期应该如下使用:

  SntpClient client = new SntpClient(); 
if(client.requestTime(time.foo.com)){
long now = client.getNtpTime()+ SystemClock.elapsedRealtime() -
client.getNtpTimeReference();
日期current = new Date(now);
Log.i(NTP tag,current.toString());
}


I would like to obtain the date and time from my own time server (i.e. 192.168.1.X) for the use inside my Android application

I have searched in the web and there are two solutions:

solution 1

solution 2

However, I was wonder if there is any solution without importing any libraries?

Thanks!!

解决方案

I end up using this one.

Create another class and then add the code inside. And then calling the class from my main activity to obtain the date.

The code is a bit old and I modified it by adding AsyncTask... works fine for me...

And one more thing, the usage of the class (from what the code sugguest) gives String.valueof(now) returns 0. Date should be used as below:

SntpClient client = new SntpClient();
if (client.requestTime("time.foo.com")) {
    long now = client.getNtpTime() + SystemClock.elapsedRealtime() - 
    client.getNtpTimeReference();
    Date current = new Date(now);
    Log.i("NTP tag", current.toString());
}

这篇关于Android:从NTP服务器获取日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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