Class VirtualClock
- java.lang.Object
-
- eu.woolplatform.utils.datetime.VirtualClock
-
public class VirtualClock extends Object
The virtual clock can be used for simulations. It can run in system mode, where it just returns the same time as the system clock, or it can run in virtual mode, where the clock can run at a different speed and a different anchor time.By default the virtual clock runs in system mode (virtual time equals system time, speed is 1). When you set the virtual anchor time or speed to another value, the clock will run in virtual mode.
The virtual anchor time is a point where a specified virtual time is anchored to the current system time at that point. If the speed is 1, this just defines a time shift.
Example with speed 1:
We may set the virtual anchor time to 10:00 when the system time is 9:30. This defines a time shift of 30 minutes in advance. When the system time is 10:30, the virtual time is 11:00.Example with speed 2:
Again we set the virtual anchor time to 10:00 when the system time is 9:30. When the system time is 10:30, one hour in system time has elapsed since the anchor time. Because of speed 2, that is 2 hours in virtual time, which we add to the virtual anchor time. So at system time 10:30, the virtual time is 12:00.
-
-
Constructor Summary
Constructors Constructor Description VirtualClock()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longcurrentTimeMillis()Returns the current time in milliseconds since the epoch.org.joda.time.LocalDategetDate()Returns the current date.org.joda.time.DateTimegetTime()Returns the current time as aDateTime.booleanisVirtualMode()Returns whether the clock is currently in virtual mode.voidsetSpeed(float speed)Sets the speed of the virtual clock.voidsetVirtualAnchorTime(Date time)Sets the virtual start time.
-
-
-
Method Detail
-
isVirtualMode
public boolean isVirtualMode()
Returns whether the clock is currently in virtual mode.- Returns:
- true if the clock is in virtual mode, false otherwise
-
setVirtualAnchorTime
public void setVirtualAnchorTime(Date time)
Sets the virtual start time. This will anchor the specified virtual time to the current system time. If you set this to null, the current virtual time will be the same as the current system time. This still serves as an anchor when the speed is not 1.- Parameters:
time- the virtual start time or null
-
setSpeed
public void setSpeed(float speed)
Sets the speed of the virtual clock. This will redefine an anchor at the current point in time. It will anchor the current virtual time to the current system time.- Parameters:
speed- the speed
-
currentTimeMillis
public long currentTimeMillis()
Returns the current time in milliseconds since the epoch. This is a virtual time or the system time, depending on the current mode of the virtual clock.- Returns:
- the current time
-
getDate
public org.joda.time.LocalDate getDate()
Returns the current date.- Returns:
- the current date
-
getTime
public org.joda.time.DateTime getTime()
Returns the current time as aDateTime.- Returns:
- the current time
-
-