views:

250

answers:

2

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!

A: 

Normally servers sync their time regulary with online time servers. So your server time should be accurate. If not, contact your admin.

To get e.g. the GMT time, use the Calendar and TimeZone classes.

ZeissS
I'm not running my Java application on a server. It is a desktop app.
Insectatorious
Well, your desktop does so too. Atleast, the default windows installation does (and afaik linux systems too)
ZeissS
+2  A: 

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.

aioobe