0

What i'm trying to achieve is to return a time that is not the system time returned by System.currentTimeMillis method. So i have a time returned by some api and i must return it when system.currentTimeMillis() is called.

As not all code is under my control, i cannot use Clock interface for example and other. (for example some lib that called system are not under my control).

I found an old issue, but i have few questions as i'm trying to override the system class.

So, Should i override entire system class and provide my own implementation of system ?

Could java.lang.System and an other implementation cohabit ?

Does i need to had something else to java command (options) ...

As you can see i'm a little bit stuck.

Thanks by advance for your help.

Otman El
  • 49
  • 4
  • 1
    Could you just not call `System.currentTimeMillis()`, and call the other method instead? One of the reasons why static binding is bad is that it makes it harder to substitute things when you need them. Essentially, the answer is "no, you can't do this". – Andy Turner May 13 '20 at 11:27
  • No, this is what the `Clock` interface is for. So if the code doesn't use it, and isn't under your control, then you're kind of stuck. – Dawood ibn Kareem May 13 '20 at 11:33
  • You can always create your own `JDK` release where you change the behavior of `System.currentTimeMillis()`. But this would be a rather hard core approach :) – Oo.oO May 14 '20 at 11:46
  • Thanks all. If i began a project, i think cloack interface is a good apporach but currently not all code is under my control..So, As described by Kayaman who said this post is a duplicate of this [post](https://stackoverflow.com/questions/18239859/java-lang-system-currenttimemillis-replace-method), i think using aspectJ is a goof approach and the latest i have. I'll try it asap in order to share my feedback. Thanks :) – Otman El May 14 '20 at 12:18

0 Answers0