When using Timeshifter with Timer, new Date() returns current date (no more shifted) after a while, although System.currentTimeMillis() is still shifted.
class TS extends TimerTask {
public void run() {
Date d = new Date();
System.out.println(" TimerTask.run() at new Date()=" + d + " (ms=" + d.getTime() + ")" +
" at new Date(S.cTM())=" + new Date(System.currentTimeMillis()));
}
public static void main(String args[]) throws Exception {
TimerTask tt = new TS();
Timer t = new Timer();
t.scheduleAtFixedRate(tt, 0, 1*3000);
System.out.println("Value of property timeshift.date: " + System.getProperty("timeshift.date"));
while( true ) {
System.out.print("Timeshift: S.cTM=" + (System.currentTimeMillis() / 1000));
System.out.print(", Date()=");
System.out.print(new Timestamp(new Date().getTime()));
System.out.print(", Date(S.cTM())=");
System.out.print(new Timestamp(new Date(System.currentTimeMillis()).getTime()));
System.out.print(", nT=" + System.nanoTime());
Date realDate = new Date();
realDate.setTime(TimeShifterImplementation.realTimeMillis());
System.out.println("; RT=" + new Timestamp(realDate.getTime()));
Thread.sleep(10000);
}
}
}
2 outputs (when new Date() returns real date), when run with: java -Dtimeshift.date="2018-09-09 09:00:00" -cp . -javaagent:timeshifter-javaagent-1.0.1.jar TS
Timeshift: S.cTM=1536455270, Date()=2018-09-09 09:07:50.23, Date(S.cTM())=2018-09-09 09:07:50.23, nT=910298106924580; RT=2018-08-05 13:28:02.356
TimerTask.run() at new Date()=Sun Sep 09 09:07:51 CST 2018 (ms=1536455271171) at new Date(S.cTM())=Sun Sep 09 09:07:51 CST 2018
TimerTask.run() at new Date()=Sun Sep 09 09:07:54 CST 2018 (ms=1536455274170) at new Date(S.cTM())=Sun Sep 09 09:07:54 CST 2018
TimerTask.run() at new Date()=Sun Aug 05 13:28:09 CST 2018 (ms=1533446889296) at new Date(S.cTM())=Sun Sep 09 09:07:57 CST 2018
TimerTask.run() at new Date()=Sun Aug 05 13:28:12 CST 2018 (ms=1533446892297) at new Date(S.cTM())=Sun Sep 09 09:08:00 CST 2018
Timeshift: S.cTM=1536455270, Date()=2018-09-09 09:07:50.244, Date(S.cTM())=2018-09-09 09:07:50.244, nT=910899110035431; RT=2018-08-05 13:38:03.358
TimerTask.run() at new Date()=Sun Sep 09 09:07:51 CST 2018 (ms=1536455271169) at new Date(S.cTM())=Sun Sep 09 09:07:51 CST 2018
TimerTask.run() at new Date()=Sun Sep 09 09:07:54 CST 2018 (ms=1536455274170) at new Date(S.cTM())=Sun Sep 09 09:07:54 CST 2018
TimerTask.run() at new Date()=Sun Aug 05 13:38:10 CST 2018 (ms=1533447490286) at new Date(S.cTM())=Sun Sep 09 09:07:57 CST 2018
TimerTask.run() at new Date()=Sun Aug 05 13:38:13 CST 2018 (ms=1533447493285) at new Date(S.cTM())=Sun Sep 09 09:08:00 CST 2018
After 09:57:54, new Date() returns real dates, but currentTimeMillis is still shifted.
If scheduleAtFixedRate period is smaller, it occurs faster.