Package org.hawaiiframework.time
Class HawaiiTime
- java.lang.Object
-
- org.hawaiiframework.time.HawaiiTime
-
public class HawaiiTime extends Object
This class provides a application wideClock
reference to instantiate newjava.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 theSystem
time.Furthermore this class should be injected using constructor injection. This will provide the flexibility to test this object using the the fixed clock methods.
- Since:
- 2.0.0
- Author:
- Marcel Overdijk
-
-
Field Summary
Fields Modifier and Type Field Description protected static ZoneId
DEFAULT_ZONE
The default zone.
-
Constructor Summary
Constructors Constructor Description HawaiiTime()
Constructs 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Long
between(Instant time)
Calculates the difference between this instance and the providedInstant
.Clock
getClock()
Returns the clock used by thisHawaiiTime
.ZoneId
getZone()
Returns the zone used by thisHawaiiTime
.Instant
instant()
Returns the currentInstant
.LocalDate
localDate()
Returns the currentLocalDate
.LocalDateTime
localDateTime()
Returns the currentLocalDateTime
.LocalTime
localTime()
Returns the currentLocalTime
.long
millis()
Returns the currentmillis
.MonthDay
monthDay()
Returns the currentMonthDay
.OffsetDateTime
offsetDateTime()
Returns the currentOffsetDateTime
.OffsetTime
offsetTime()
Returns the currentOffsetTime
.void
setClock(Clock clock)
Sets the clock to be used by thisHawaiiTime
.void
setZone(ZoneId zone)
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
useSystemClock()
Sets the system clock to be used.Year
year()
Returns the currentYear
.YearMonth
yearMonth()
Returns the currentYearMonth
.ZonedDateTime
zonedDateTime()
Returns the currentZonedDateTime
.
-
-
-
Field Detail
-
DEFAULT_ZONE
protected static final ZoneId DEFAULT_ZONE
The default zone.
-
-
Constructor Detail
-
HawaiiTime
public HawaiiTime()
Constructs a newHawaiiTime
with the defaultDEFAULT_ZONE
.
-
HawaiiTime
public HawaiiTime(ZoneId zone)
Constructs a newHawaiiTime
with the supplied zone.- Parameters:
zone
- the zone, not null
-
HawaiiTime
public HawaiiTime(Clock clock)
Constructs a newHawaiiTime
with the supplied clock. Also theZoneId
is derived from the supplied clock.- Parameters:
clock
- the clock, not null
-
-
Method Detail
-
getClock
public Clock getClock()
Returns the clock used by thisHawaiiTime
.- Returns:
- the clock
-
setClock
public void setClock(Clock clock)
Sets the clock to be used by thisHawaiiTime
.- Parameters:
clock
- the clock, not null
-
getZone
public ZoneId getZone()
Returns the zone used by thisHawaiiTime
.- Returns:
- the zone
-
setZone
public void setZone(ZoneId zone)
Sets the zone to be used by thisHawaiiTime
.- Parameters:
zone
- the zone
-
between
public Long between(Instant time)
Calculates the difference between this instance and the providedInstant
.- Parameters:
time
- which to compare- Returns:
- the result of the of the difference calculation as a
Long
-
useFixedClock
public void useFixedClock(Clock clock)
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
public void useFixedClock(LocalDateTime dateTime)
Sets a fixed clock to be used.- Parameters:
dateTime
- the fixed date time, not null
-
useFixedClock
public void useFixedClock(OffsetDateTime dateTime)
Sets a fixed clock to be used.- Parameters:
dateTime
- the fixed date time, not null
-
useFixedClock
public void useFixedClock(ZonedDateTime dateTime)
Sets a fixed clock to be used.- Parameters:
dateTime
- the fixed date time, not null
-
useFixedClock
protected void useFixedClock(Instant instant)
Sets a fixed clock to be used.- Parameters:
instant
- the fixed instant, not null
-
useFixedClock
protected void useFixedClock(Instant instant, ZoneId zone)
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
public Instant instant()
Returns the currentInstant
.- Returns:
- the
Instant
-
localDate
public LocalDate localDate()
Returns the currentLocalDate
.- Returns:
- the
LocalDate
-
localDateTime
public LocalDateTime localDateTime()
Returns the currentLocalDateTime
.- Returns:
- the
LocalDateTime
-
localTime
public LocalTime localTime()
Returns the currentLocalTime
.- Returns:
- the
LocalTime
-
millis
public long millis()
Returns the currentmillis
.- Returns:
- the
millis
-
monthDay
public MonthDay monthDay()
Returns the currentMonthDay
.- Returns:
- the
MonthDay
-
offsetDateTime
public OffsetDateTime offsetDateTime()
Returns the currentOffsetDateTime
.- Returns:
- the
OffsetDateTime
-
offsetTime
public OffsetTime offsetTime()
Returns the currentOffsetTime
.- Returns:
- the
OffsetTime
-
year
public Year year()
Returns the currentYear
.- Returns:
Year
-
yearMonth
public YearMonth yearMonth()
Returns the currentYearMonth
.- Returns:
- the
YearMonth
-
zonedDateTime
public ZonedDateTime zonedDateTime()
Returns the currentZonedDateTime
.- Returns:
- the
ZonedDateTime
-
-