Override Java System.currentTimeMillis

Posted by Mike Clark on Stack Overflow See other posts from Stack Overflow or by Mike Clark
Published on 2010-01-04T19:39:53Z Indexed on 2010/04/03 18:33 UTC
Read the original article Hit count: 289

Filed under:
|

Is there a way, either in code or with JVM arguments, to override the current time, as presented via System.currentTimeMillis, other than manually changing the system clock on the host machine?

A little background:

We have a system that runs a number of accounting jobs that revolve much of their logic around the current date (ie 1st of the month, 1st of the year, etc)

Unfortunately, a lot of the legacy code calls functions such as new Date() or Calendar.getInstance(), both of which eventually call down to System.currentTimeMillis.

For testing purposes, right now, we are stuck with manually updating the system clock to manipulate what time and date the code thinks that the test is being run.

So my question is:

Is there a way to override what is returned by System.currentTimeMillis? For example, to tell the JVM to automatically add or subtract some offset before returning from that method?

Thanks in advance!

© Stack Overflow or respective owner

Related posts about java

Related posts about jvm