Package org.hawaiiframework.time
Class HawaiiTime
- java.lang.Object
-
- org.hawaiiframework.time.HawaiiTime
-
public class HawaiiTime extends Object
This class provides a application wideClockreference to instantiate newjava.timedate 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
HawaiiTimeand probably instantiate date and time objects based on theSystemtime.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 ZoneIdDEFAULT_ZONEThe default zone.
-
Constructor Summary
Constructors Constructor Description HawaiiTime()Constructs a newHawaiiTimewith the defaultDEFAULT_ZONE.HawaiiTime(Clock clock)Constructs a newHawaiiTimewith the supplied clock.HawaiiTime(ZoneId zone)Constructs a newHawaiiTimewith the supplied zone.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Longbetween(Instant time)Calculates the difference between this instance and the providedInstant.ClockgetClock()Returns the clock used by thisHawaiiTime.ZoneIdgetZone()Returns the zone used by thisHawaiiTime.Instantinstant()Returns the currentInstant.LocalDatelocalDate()Returns the currentLocalDate.LocalDateTimelocalDateTime()Returns the currentLocalDateTime.LocalTimelocalTime()Returns the currentLocalTime.longmillis()Returns the currentmillis.MonthDaymonthDay()Returns the currentMonthDay.OffsetDateTimeoffsetDateTime()Returns the currentOffsetDateTime.OffsetTimeoffsetTime()Returns the currentOffsetTime.voidsetClock(Clock clock)Sets the clock to be used by thisHawaiiTime.voidsetZone(ZoneId zone)Sets the zone to be used by thisHawaiiTime.voiduseFixedClock(long millis)Sets a fixed clock to be used.voiduseFixedClock(Clock clock)Sets a fixed clock to be used.protected voiduseFixedClock(Instant instant)Sets a fixed clock to be used.protected voiduseFixedClock(Instant instant, ZoneId zone)Sets a fixed clock to be used.voiduseFixedClock(LocalDateTime dateTime)Sets a fixed clock to be used.voiduseFixedClock(OffsetDateTime dateTime)Sets a fixed clock to be used.voiduseFixedClock(ZonedDateTime dateTime)Sets a fixed clock to be used.voiduseSystemClock()Sets the system clock to be used.Yearyear()Returns the currentYear.YearMonthyearMonth()Returns the currentYearMonth.ZonedDateTimezonedDateTime()Returns the currentZonedDateTime.
-
-
-
Field Detail
-
DEFAULT_ZONE
protected static final ZoneId DEFAULT_ZONE
The default zone.
-
-
Constructor Detail
-
HawaiiTime
public HawaiiTime()
Constructs a newHawaiiTimewith the defaultDEFAULT_ZONE.
-
HawaiiTime
public HawaiiTime(ZoneId zone)
Constructs a newHawaiiTimewith the supplied zone.- Parameters:
zone- the zone, not null
-
HawaiiTime
public HawaiiTime(Clock clock)
Constructs a newHawaiiTimewith the supplied clock. Also theZoneIdis 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
-
-