1

I have this scenario at my work where I need to run my java Application with JAVA made to believe that date/time is somewhere in the future. Team has been doing this my changing the System time and restarting the WebSphere Application Sever to reflect the required date/Time needed. However, I felt this is a crud way to doing this "Time-Travel". I have searched around and found there is a way to set the TimeZone of the server at start-up by using "Duser.timezone=GMT" in the JVM Argument. Is there anyway to make Java on my workstation to point to custom date/time other than changing my System Date/Time?

Sunny
  • 11
  • 1
  • Also duplicate of [this](http://stackoverflow.com/q/2001671/642706) and [this](http://stackoverflow.com/q/27067049/642706) and several others. Please search Stack Overflow thoroughly before posting. – Basil Bourque Apr 29 '16 at 04:57

2 Answers2

1

The best practise would be not messing up with the system clock. You should do some refactoring of your code instead. For example to make your method use Clock.getInstance() (System.currentTimeMillis() -> Clock.getInstance().currentTimeMillis() / myClockInstance.currentTimeMillis()).

Mateusz Stefaniak
  • 856
  • 1
  • 9
  • 20
  • If Java 8 is not an option, Guava has a similar [`Ticker`](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Ticker.html) class. – shmosel Apr 29 '16 at 00:15
  • [Working link to `Ticker`](https://google.github.io/guava/releases/20.0/api/docs/com/google/common/base/Ticker.html) – Corwin Newall Apr 23 '19 at 23:32
0

Actually there are messy ways you could try:

1) Simply write your own java.util.Date class and hope it gets loaded before the JVM.

2) Tune the ClassLoader or write a custom one, try to get some special care for that java.util.Date

3) You might also consider playing with the different Calendars.

4) Do what Mateusz Stefaniak pointed out

5) Manipulate the original Java jars

There may even be more ways that I didnt think of atm...

JayC667
  • 2,418
  • 2
  • 17
  • 31