Class HawaiiTime


  • public class HawaiiTime
    extends Object
    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 the fixed clock methods.

    Since:
    2.0.0
    Author:
    Marcel Overdijk
    • Field Detail

      • DEFAULT_ZONE

        protected static final ZoneId DEFAULT_ZONE
        The default zone.
    • Constructor Detail

      • HawaiiTime

        public HawaiiTime()
        Constructs a new HawaiiTime with the default DEFAULT_ZONE.
      • HawaiiTime

        public HawaiiTime​(ZoneId zone)
        Constructs a new HawaiiTime with the supplied zone.
        Parameters:
        zone - the zone, not null
      • HawaiiTime

        public HawaiiTime​(Clock clock)
        Constructs a new HawaiiTime with the supplied clock. Also the ZoneId is derived from the supplied clock.
        Parameters:
        clock - the clock, not null
    • Method Detail

      • getClock

        public Clock getClock()
        Returns the clock used by this HawaiiTime.
        Returns:
        the clock
      • setClock

        public void setClock​(Clock clock)
        Sets the clock to be used by this HawaiiTime.
        Parameters:
        clock - the clock, not null
      • getZone

        public ZoneId getZone()
        Returns the zone used by this HawaiiTime.
        Returns:
        the zone
      • setZone

        public void setZone​(ZoneId zone)
        Sets the zone to be used by this HawaiiTime.
        Parameters:
        zone - the zone
      • between

        public Long between​(Instant time)
        Calculates the difference between this instance and the provided Instant.
        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 null
        zone - the fixed zone, not null
      • useSystemClock

        public void useSystemClock()
        Sets the system clock to be used.
      • instant

        public Instant instant()
        Returns the current Instant.
        Returns:
        the Instant
      • localDate

        public LocalDate localDate()
        Returns the current LocalDate.
        Returns:
        the LocalDate
      • localDateTime

        public LocalDateTime localDateTime()
        Returns the current LocalDateTime.
        Returns:
        the LocalDateTime
      • localTime

        public LocalTime localTime()
        Returns the current LocalTime.
        Returns:
        the LocalTime
      • millis

        public long millis()
        Returns the current millis.
        Returns:
        the millis
      • monthDay

        public MonthDay monthDay()
        Returns the current MonthDay.
        Returns:
        the MonthDay
      • offsetDateTime

        public OffsetDateTime offsetDateTime()
        Returns the current OffsetDateTime.
        Returns:
        the OffsetDateTime
      • offsetTime

        public OffsetTime offsetTime()
        Returns the current OffsetTime.
        Returns:
        the OffsetTime
      • year

        public Year year()
        Returns the current Year.
        Returns:
        Year
      • yearMonth

        public YearMonth yearMonth()
        Returns the current YearMonth.
        Returns:
        the YearMonth
      • zonedDateTime

        public ZonedDateTime zonedDateTime()
        Returns the current ZonedDateTime.
        Returns:
        the ZonedDateTime