• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

system time/clock

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic