Java:从服务器获取当前日期和时间不是系统时钟 [英] Java: Get current Date and Time from Server not System clock
问题描述
Date date = new Date();
这给了我根据主机的系统时钟的当前日期和时间。有没有什么办法可以从在线服务器获取当前的日期和时间?世界时间服务器可能吗?
我看过这篇文章,它描述了我想要什么,但是恐怕我需要比在那里提供的帮助更多的帮助。
简而言之,我想得到一个不依赖主机系统时钟的日期和时间。
谢谢!
您可以查看
$ b $中的Java NTP客户端演示bhttp://www.docjar .com / html / api / examples / ntp / NTPClient.java.html
和一些使用此客户端的示例代码
http://www.docjar .com / html / api / examples / ntp / TimeClient.java.html
大约有170行记录良好的java代码。
In my Java program, I need to create an instance of the current moment in time. I use
Date date = new Date();
This gives me the current date and time as per the host machine's system clock. Is there any way I can get the current date and time from an online server? The world time server perhaps?
I have seen this post and it describes what I want but I'm afraid I need more help than what's provided there.
In a nutshell, I want to get a date and time that is not dependant on the host machine's system clock.
Thanks!
You could have a look at the Java NTP Client demo available at
http://www.docjar.com/html/api/examples/ntp/NTPClient.java.html
and some example code that utilizes this client
http://www.docjar.com/html/api/examples/ntp/TimeClient.java.html
It's about 170 lines of well documented java code.
这篇关于Java:从服务器获取当前日期和时间不是系统时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!