Patel
Patel

Reputation: 21

How to get today's date in java if system date is one year back of current date?

My motive is to get current date without caring of the system date.

Upvotes: 1

Views: 210

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338201

Time Server

Access a time server over the Internet. Have your app contact that local time server to query the current time.

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

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 radio clock via USB 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 or GLONASS.

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.

Caveat: Not practical in Seattle.

photo of a sundial on a pedestal in a garden


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.

Upvotes: 0

StuPointerException
StuPointerException

Reputation: 7267

My suggestion, if you really can't rely on the system time is to use a time server. Apache commons has a really useful client which will help with the this. There are plenty of examples online:

https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ntp/NTPUDPClient.html

There are also numerous available timeservers.

Don't be tempted to just correct the date yourself in the code, this will be fragile and will break if someone corrects the server time.

The obvious, and easier, solution is to correct the time on the server though!

Upvotes: 6

Related Questions