Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes system time/clock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "system time/clock" Watch "system time/clock" New topic
Author

system time/clock

Ed Zeval
Greenhorn

Joined: Jun 19, 2008
Posts: 21
Hi,

Is there a way to change the system clock? I just want to be able to change it for some tests. How would you change it back to the correct time?

Thanks!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42281
    
  64
You mean programmatically in Java? I don't think there is, unless you want to use JNI to access system routines.

Manually changing the clock is not sufficient for your purposes?


Ping & DNS - my free Android networking tools app
Ed Zeval
Greenhorn

Joined: Jun 19, 2008
Posts: 21
Yeah.. i mean programmatically..

Manually wouldn't work since I want to automate these tests..

If I can't actually change the clock, is there a way to fake/mock it?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

I am assuming that these tests are started with shellscripts? If so, you can add a call to the "date" command, prior to the call to start the JVM, to set the date. To set it back, you can use the "rdate" command to a host with a time server.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

As with most things in computing, this is probably easy to solve with another layer of indirection.

Everywhere in your code that you now say, e.g., "new Date()" or System.currentTimeMillis(), or however you determine what time it is, replace those with something like "TimeFactory.get().newDate()" or "TimeFactory.get().currentTimeMillis()". TimeFactory.get() returns an object that has methods like newDate(), currentTimeMillis(), or whatever else you need, declared in an interface "ITime" or something.

get() looks like (This leaves out exception handling and also ignores that the System property might be null; you need to deal with those issues



Now, you can change the ITime object -- and therefore how the whole app tells time -- with a system property. You can implement one that reports a fixed time, or a delayed time, or an accelerated time, or whatever you need.


[Jess in Action][AskingGoodQuestions]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
I think setDefaultTimezone() resets the timezone of the JVM that's currently running ... but that won't help if you want a different month or something like that.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: system time/clock