Java:从服务器而不是系统时钟获取当前日期和时间 [英] Java: Get current Date and Time from Server not System clock

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

问题描述

在我的 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.

谢谢!

推荐答案

您可以查看位于

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

大约有 170 行记录良好的 Java 代码.

It's about 170 lines of well documented java code.

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

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