3

I need to make sure that in my client application the user doesn't dramatically change the machine date (e.g. bringing it to 1 month before).

To the best of my knowledge, the best way to check the current time in Java is

System.currentTimeInMillis()

How can I make sure that this is the correct date, in a multiplatform environment (either Windows, Linux, Mac, etc.)?

No connection to remote servers available, nor to the internet. Also, at startup time the time could be already tampered. I just need to check the time for internal biz logic purpose, not to set anything.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Cristiano Ghersi
  • 1,944
  • 1
  • 20
  • 46
  • There are plenty of ways. One of them could be storing current data on every application startup or update it every now and then. If your app connects to some remote server, compare server and app time. – itwasntme Aug 25 '15 at 01:37
  • How would you act using this information? Java does not have an API for setting the system time. – Tim Biegeleisen Aug 25 '15 at 01:37
  • 2
    You could (presumably) use [ntp](http://mvnrepository.com/artifact/org.apache.directory.server/apacheds-protocol-ntp/2.0.0-M15) and "us.pool.ntp.org" (or just "pool.ntp.org" if you're unsure of the user's country code). – Elliott Frisch Aug 25 '15 at 01:39
  • Thank you everyone, some comments on your answers. No connection to remote servers available, nor to the internet. Also, at startup time the time could be already tampered. I just need to check the time for internal biz logic purpose, not to set anything. – Cristiano Ghersi Aug 25 '15 at 01:45
  • It's not a concern of java; it's an OS responsibility. – Bohemian Aug 25 '15 at 01:51
  • 1
    If you don't have a way to communicate with an outside source of information (such as a server on the Internet), then there's no way to verify that the system time is correct, because there's no way to determine what the real time is. Your program is completely dependent on the information that the host OS provides. – Wyzard Aug 25 '15 at 01:52

3 Answers3

4

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. Similarly, a receiver of GPS signals could also capture the time signal.

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

photo of a couple of radio clock via USB devices

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 applicable in Seattle.

photo of a sundial on a pedestal in a garden

Black Box, No Inputs = No Go

More seriously, if you do not trust the system time, you have no access to a time server, and you cannot ask the user for current time, I cannot imagine any solution. You have a black box with no inputs. The radio clock via USB is adding such an input.

Wikipedia has an article on Clock synchronization.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2

If this is for "demo period" purposes, and you're worried the user might disallow NTP requests (or shut down Internet access), you're out of luck. You might try to detect clues of tampering with time (like files created in the future, or starting the application at a time that is earlier than the previous run's recorded finish time), or you can measure usage time as opposed to period of use (i.e. instead of giving the users 30 days of free usage, you can give them 12 hours of application time, however they break it down), as you can confidently measure time spent inside your application.

Or, you could trust your users. Some might be pirates, but piracy is often a thing of convenience, and most people will not bother changing the clock just to be able to use your software. (This, of course, only applies to some applications; you don't want to trust users if it's a banking or bookkeeping app, for example.)

Amadan
  • 191,408
  • 23
  • 240
  • 301
1

The solution is nothing related to java. What common solution for this is using NTP server, and configure the system/OS to always synchronize its time with NTP remote server. NTP also used to synchronize the clocks of servers in a cluster. For more information, You can consult http://www.ntp.org/ntpfaq/NTP-s-def.htm

Ferdinand Neman
  • 680
  • 3
  • 6