tags:

views:

544

answers:

7

Hello

i was wondering if i could have any help in creating a system.

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 conveinience of changing the time by the odd minute.

i also need to incorprate the changes that the clock makes e.g ( going forward an hour or back an hour at certain times of the Year (Britsh Time))

i am not sure if there is already a java system that does this that i could look at but i literarly dont no where to start with this.

Any help would be greatly appreciated.

Thanks Dave

A: 

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.

Tobias Langner
A: 

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.

kd304
+1  A: 

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.

Nate
+1  A: 

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.

izb
A: 

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.

Adam Crume
A: 

Thanks for the replies however i have been told that it is possible to do this just by storing the time and date into a file and running rules on it.

iss it possible for someone to please show me code that can do this as it is needed for research to see if java can be used for this type of system

A: 

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.

Diego