Reputation:
I am looking at the idea of a Java project that detects when the computer clock has been changed. The idea is to record the date and time on the computer clock and store it in a file. I then want to be able to run rules on it that shows a message if the clock has been changed. I am also looking at incorporating rules such as "Allowing no more than 5 minute changes". This is to allow convenience of changing the time by the odd minute.
I also need to incorporate the changes that the clock makes. E.g., going forward an hour or back an hour at certain times of the year (British Time).
I am not sure if there is already a Java system that does this that I could look at but I literally don't know where to start with this.
Upvotes: 5
Views: 6316
Reputation: 339342
You seem to be asking for two very different things:
For the first, no good simple way to do that. You could occasionally sample the current date-time, record it, then sample again later. You would do this in UTC to avoid mistaking DST autumn changes as a user/sysadmin alteration.
Example code using the java.time classes. Avoid the old outmoded non-java.time classes as they are poorly designed, confusing, and troublesome.
Instant now1 = Instant.now(); // Record, run again, compare.
String now1AsString = now1.toString(); // Ex: 2011-12-03T10:15:30.724Z
…
Instant now2 = Instant.now();
Boolean timeWentBackwards = now2.isBefore( now1 );
Of course you cannot use this technique for adjusting into the future, only the past. The clock is always moving into the future.
The only reliable way to detect molestation of the clock is to have more than one clock. You need have trusted access (connection) to a trusted clock, probably either a time server on your network or a time server on the Internet. Even then, having only a pair of clocks can be tricky. As the old sea-faring adage says, “Bring aboard one compass, or three.”.
Detecting DST changes should be irrelevant. The general best practice in handling date-time is to work in UTC. This includes most of your business logic, your data storage, and your data exchange.
Remember: The time-of-day rolling forward or backward with DST is meaningless -- time continued to flow, not warp, with the same number of seconds passing.
Upvotes: 0
Reputation: 17150
If I understand your question right, then what you are trying to do assumes that initially, the system clock is at the optimal state. Then, after every minute you retrieve the last entry in the file (either the last or the first, whatever method you chose) and make sure that the difference between the two times is either 60 seconds or any other time interval you choose.
Example: At time 0, you place in the file 12:00:00 At time 1, you compare 12:01:00 (which you retrieve from the system clock) with 12:00:00 (which you retrieve from file)and deem that it is alright and write it to the file. ... At some time n, you compare xx:yy:zz whose difference with the last entry is not 60 seconds, so a change has been detected. ...
I believe the rules you're talking about involves taking the last entry from the file and doing a comparison with the current time you retrieved from the system clock. Whether or not this is a good method however, is arguable.
Upvotes: 1
Reputation: 15834
I haven't tried this, but it might work to have a background thread which wakes up once a minute, gets the current time, and compares it to what it last saw. Theoretically, the difference should be very close to one minute. If it differs much from that, that would mean the clock changed.
Upvotes: 0
Reputation: 51820
If you can rely on the presence of a network connection, you could compare the time with the time from an internet time server (CBATG).
Simply convert the local time and a fetched time to the same time zone and store the difference. If the diff changes by more than the allowed amount of 5 minutes, you know it's been modified.
Upvotes: 1
Reputation: 16898
Java gets all it's time/date information from the system clock anyway, so there's no way of knowing if the underlying system clock has been changed. If you take the date and time and store it in a file - that will detect if the time has been set back (i.e. - the time in the file is after the current time, and no DST has occurred) - but it can't detect if time has been set forward (i.e. - the time in the file is 2 hours before the current time - has 2 hours really elapsed, or has the user set the clock ahead by 2 hours while the program wasn't running?). The only way you could do something like this is use a known Time Server, and instead of saving the time in the file, save the offset between the time server and the local system clock.
Upvotes: 1
Reputation: 70007
A good place to use the Joda time library. In theory, you only need to check if the local timezone has moved between DST and non DST state - if you don't want to check for local time shifts.
Upvotes: 0
Reputation: 10828
First: The clock changes veeeery often. Try using java.util.Timer for updating. Calender is for your time system.
http://java.sun.com/javase/6/docs/api/ is your friend in this.
Start small, one step at a time. That's my advice.
Upvotes: 0