Java: Get current Date and Time from Server not System clock
Asked Answered
D

3

10

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!

Dervish answered 12/5, 2010 at 9:27 Comment(1)
Similar: how to make my java app get global time from some online clockUnsettle
U
17

Local network

If your machine is connected to a local network, connect to some machine acting as a trustworthy time server. You could write your own such time server.

Network Time Protocol

Or you could use the well-worn Network Time Protocol (NTP) with existing server and client implementations bundled with most any OS. For more on this, see the accepted Answer by aioobe.

Consider placing that time server computer in network’s DMZ so as to updates from time servers on the Internet such as the pool.ntp.org project or those provided by the United States federal government (NIST).

Internet

If your machine is connected to the internets, connect to a trustworthy time server. As discussed above, use a custom protocol such as a web service, or use NTP.

This is discussed in the Question, how to make my java app get global time from some online clock.

See also Questions: Java NTP client, and Java client for time server, such as Network Time Protocol (NTP).

Radio Clock

Obtain a radio clock with a USB connection for output of current time synchronized by a time code transmitted by a radio transmitter connected to a time standard such as an atomic clock. Transmitters are broadcasting in many countries all over the world.

The Meinberg Global company, at least, offers several such devices.

photo of a couple of external radio clock devices

GPS

Similar to the radio clocks above, a receiver of GPS (Global Positioning System) signals might also capture and relay the time signal. Or perhaps GALILEO, GLONASS or other Radionavigation-satellite service.

Sundial

Position a sundial outside a window. Attach a webcam to the computer in question. Position the webcam in the window. Write an app to interpret the time of day from current image of the sundial.

photo of a sundial on a pedestal in a garden

Caveat: This solution does not function in Seattle.


java.time.Clock

To harness any of these suppliers of the current moment, write a subclass of the abstract java.time.Clock class.

You can pass your Clock implementation as an argument to the various java.time methods. For example, Instant.now( clock ).

Instant instant = Instant.now( yourClockGoesHere ) ;

For testing purposes, note the alternate implementations of Clock available statically from Clock itself: fixed, offset, tick, and more.

Avoid the legacy date-time classes from the earliest versions of Java, such as Date & Calendar. These troublesome classes are entirely supplanted by the java.time classes.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Unsettle answered 1/3, 2018 at 22:2 Comment(1)
Upvoted for the sundial, which is obviously the correct answer.Antoniaantonie
D
7

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.

Divisionism answered 12/5, 2010 at 9:32 Comment(0)
S
-4

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.

Salep answered 12/5, 2010 at 9:29 Comment(3)
I'm not running my Java application on a server. It is a desktop app.Dervish
Well, your desktop does so too. Atleast, the default windows installation does (and afaik linux systems too)Salep
Yes, The desktop syncs time , but it is subjected to tampering which makes it unreliable at times.Kudu

© 2022 - 2024 — McMap. All rights reserved.