Java – how do I use the Internet time server to get time?

I want GMT [Greenwich mean time], and I don't want to rely on my system date and time Basically, I want to use a time synchronization server, such as in pool. ntp. Org [India] for GMT calculation, or it may be my wrong direction!

Is there any Java library that can get time from the time server?

Solution

Sp0d incorrect:

timeInfo.getReturnTime(); // Returns time at which time message packet was received by local machine

So it just returns the current system time, not the received time See timeinfo man page You should use

timeInfo.getMessage().getTransmitTimeStamp().getTime();

Replace So the code block will be:

String TIME_SERVER = "time-a.nist.gov";   
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
Date time = new Date(returnTime);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>