Java-Fake system clock in launch

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

Answers


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.


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.


Need Your Help

SQL Server: Convert a month name (string) to a date

sql-server datetime types type-conversion

I have a table with a month name in it as type varchar. e.g. "November". Can I convert this to a date field?