0

For a workaround , I need to change the system clock for a certain java program without touching the code in order to get for :

calendar.getinstance().get(calendar.year) = 2013

is it possible just with setting a variable through launch configuration?

Thanks

EDIT :Sorry , i want to simulate the current year as 2013

bazic
  • 255
  • 2
  • 3
  • 9

2 Answers2

1

First, without admin rights it is not possible to set the system clock on OS level. This would also affect all software running on the machine. But you want to change the clock just for one program. So when you deny this option of setting the system clock then it follows that any kind of code change is necessary (what you don't want). You cannot fake the system time just by configuration properties without any code change.

Second, the idea of mocking java.util.Date (writing a subclass) might work for you, although it requires code change. See this interesting link using instrumentation.

Third, the new JSR-310 coming in Java 8 has a special class java.time.Clock which allows to be injected in existing code and can simulate a fake clock. The advantage of this solution is: Once you have prepared your program this way then you can indeed change the clock later just by configuration. But at least once you need an initial code change.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • Related to this, if the OP was using [JodaTime](http://www.joda.org/joda-time/) (the precursor to JSR310, essentially), there's a similar set of functions allowing you to change the clock. – Clockwork-Muse Jan 03 '14 at 09:15
  • @Clockwork-Muse Indeed, JodaTime offers the interface `org.joda.time.DateTimeUtils.MillisProvider`. I regret that JSR-310-clock is an abstract class, not an interface. Stephen Colebourne made recently the statement that it is too late to change this for Java 8. – Meno Hochschild Jan 03 '14 at 10:36
0

If you are on Linux you can try to fake the time with a library: http://archive09.linux.com/feature/147801 .

I never tried this and I don't know if this works with Java.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111