Package org.hawaiiframework.time
Class HawaiiTime
java.lang.Object
org.hawaiiframework.time.HawaiiTime
This class provides a application wide
Clock
reference to instantiate new
java.time
date and time objects.
It provides convenient methods to use a fixed date time which is particular useful for unit testing but also to change the date time in a running application to test how it behaves on a given date or time.
Be aware that third-party libraries being used by the application do obviously not use
HawaiiTime
and probably instantiate date and time objects based on the System
time.
Furthermore, this class should be injected using constructor injection. This will provide the flexibility to test this object using the fixed clock methods.
- Since:
- 2.0.0
-
Field Summary
-
Constructor Summary
ConstructorDescriptionConstructs a newHawaiiTime
with the defaultDEFAULT_ZONE
.HawaiiTime
(Clock clock) Constructs a newHawaiiTime
with the supplied clock.HawaiiTime
(ZoneId zone) Constructs a newHawaiiTime
with the supplied zone. -
Method Summary
Modifier and TypeMethodDescriptionCalculates the difference between this INSTANCE and the providedInstant
.getClock()
Returns the clock used by thisHawaiiTime
.static HawaiiTime
getZone()
Returns the zone used by thisHawaiiTime
.instant()
Returns the currentInstant
.Returns the currentLocalDate
.Returns the currentLocalDateTime
.Returns the currentLocalTime
.long
millis()
Returns the currentmillis
.monthDay()
Returns the currentMonthDay
.Returns the currentOffsetDateTime
.Returns the currentOffsetTime
.void
Sets the clock to be used by thisHawaiiTime
.void
Sets the zone to be used by thisHawaiiTime
.void
useFixedClock
(long millis) Sets a fixed clock to be used.void
useFixedClock
(Clock clock) Sets a fixed clock to be used.protected void
useFixedClock
(Instant instant) Sets a fixed clock to be used.protected void
useFixedClock
(Instant instant, ZoneId zone) Sets a fixed clock to be used.void
useFixedClock
(LocalDateTime dateTime) Sets a fixed clock to be used.void
useFixedClock
(OffsetDateTime dateTime) Sets a fixed clock to be used.void
useFixedClock
(ZonedDateTime dateTime) Sets a fixed clock to be used.void
Sets the system clock to be used.year()
Returns the currentYear
.Returns the currentYearMonth
.Returns the currentZonedDateTime
.
-
Field Details
-
DEFAULT_ZONE
The default zone.
-
-
Constructor Details
-
HawaiiTime
public HawaiiTime()Constructs a newHawaiiTime
with the defaultDEFAULT_ZONE
. -
HawaiiTime
Constructs a newHawaiiTime
with the supplied zone.- Parameters:
zone
- the zone, not null
-
HawaiiTime
Constructs a newHawaiiTime
with the supplied clock. Also theZoneId
is derived from the supplied clock.- Parameters:
clock
- the clock, not null
-
-
Method Details
-
getInstance
-
getClock
Returns the clock used by thisHawaiiTime
.- Returns:
- the clock
-
setClock
Sets the clock to be used by thisHawaiiTime
.- Parameters:
clock
- the clock, not null
-
getZone
Returns the zone used by thisHawaiiTime
.- Returns:
- the zone
-
setZone
Sets the zone to be used by thisHawaiiTime
.- Parameters:
zone
- the zone
-
between
Calculates the difference between this INSTANCE and the providedInstant
.- Parameters:
time
- which to compare- Returns:
- the result of the difference calculation as a
Long
-
useFixedClock
Sets a fixed clock to be used.- Parameters:
clock
- the fixed clock, not null
-
useFixedClock
public void useFixedClock(long millis) Sets a fixed clock to be used.- Parameters:
millis
- the millis since epoch
-
useFixedClock
Sets a fixed clock to be used.- Parameters:
dateTime
- the fixed date time, not null
-
useFixedClock
Sets a fixed clock to be used.- Parameters:
dateTime
- the fixed date time, not null
-
useFixedClock
Sets a fixed clock to be used.- Parameters:
dateTime
- the fixed date time, not null
-
useFixedClock
Sets a fixed clock to be used.- Parameters:
instant
- the fixed instant, not null
-
useFixedClock
Sets a fixed clock to be used.- Parameters:
instant
- the fixed instant, not nullzone
- the fixed zone, not null
-
useSystemClock
public void useSystemClock()Sets the system clock to be used. -
instant
Returns the currentInstant
.- Returns:
- the
Instant
-
localDate
Returns the currentLocalDate
.- Returns:
- the
LocalDate
-
localDateTime
Returns the currentLocalDateTime
.- Returns:
- the
LocalDateTime
-
localTime
Returns the currentLocalTime
.- Returns:
- the
LocalTime
-
millis
public long millis()Returns the currentmillis
.- Returns:
- the
millis
-
monthDay
Returns the currentMonthDay
.- Returns:
- the
MonthDay
-
offsetDateTime
Returns the currentOffsetDateTime
.- Returns:
- the
OffsetDateTime
-
offsetTime
Returns the currentOffsetTime
.- Returns:
- the
OffsetTime
-
year
Returns the currentYear
.- Returns:
Year
-
yearMonth
Returns the currentYearMonth
.- Returns:
- the
YearMonth
-
zonedDateTime
Returns the currentZonedDateTime
.- Returns:
- the
ZonedDateTime
-