Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

system time/clock

 
Ed Zeval
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ed Zeval
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20894
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic