I'm using time elapsed as a variable in a program which I am currently debugging. As I am debugging this the time elapsed variable still increases whilst I am examining variables giving undesired results and makes debugging ineffective. Is there anyway to freeze time or stop the variable from changing? (I am using System.currentTimeMillis()
to calculate the time).
Edit:
I have an object in a space that moves as time increases, because I am using System.currentTimeMillis()
to calculate how much time has passed, it doesn't work in debugging. I'd like to know if there is a way I can work around this, thanks.
One common approach to this kind of problem is to use Dependency Injection. Create a class Clock (for example) which is initialized (injected) with a time source. In production code this source might well be System.nanotime() or System.currentTimeMillis(). In debugging you use a controllable clock. The debugging implementation of the Clock class has additional methods (e.g. suspend(), resume(), setClockTo()) that allow one to use simulated time.