Class TimeUtils


  • public class TimeUtils
    extends java.lang.Object
    Utilities for time and mock time.
    • Constructor Summary

      Constructors 
      Constructor Description
      TimeUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void clearMockClock()
      Clears the mock clock and causes time to tick again.
      static java.time.Instant currentTime()
      Returns the current time as an Instant, or a mocked out equivalent.
      static java.lang.String dateTimeFormat​(java.time.Instant time)
      Formats a given date+time value to an ISO 8601 string.
      static java.time.Instant earlier​(java.time.Instant time1, java.time.Instant time2)
      Determines the earlier of two instants.
      static java.time.Duration elapsedTime​(java.time.Instant start)
      Returns elapsed time between given start and current time as a Duration.
      static java.time.Instant later​(java.time.Instant time1, java.time.Instant time2)
      Determines the later of two instants.
      static java.time.Duration longest​(java.time.Duration duration1, java.time.Duration duration2)
      Determines the longest of two durations.
      static void rollMockClock​(java.time.Duration delta)
      Rolls an already set mock clock by the given duration.
      static void setMockClock()
      Sets the mock clock to the current time as a fixed instant.
      static void setMockClock​(java.time.Instant fixedInstant)
      Sets the mock clock to a fixed instant.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TimeUtils

        public TimeUtils()
    • Method Detail

      • setMockClock

        public static void setMockClock()
        Sets the mock clock to the current time as a fixed instant.
      • setMockClock

        public static void setMockClock​(java.time.Instant fixedInstant)
        Sets the mock clock to a fixed instant.
        Parameters:
        fixedInstant - a fixed instant
      • rollMockClock

        public static void rollMockClock​(java.time.Duration delta)
        Rolls an already set mock clock by the given duration.
        Parameters:
        delta - amount to roll the mock clock, can be negative
        Throws:
        java.lang.IllegalStateException - if the mock clock isn't set
      • clearMockClock

        public static void clearMockClock()
        Clears the mock clock and causes time to tick again.
      • currentTime

        public static java.time.Instant currentTime()
        Returns the current time as an Instant, or a mocked out equivalent.
      • elapsedTime

        public static java.time.Duration elapsedTime​(java.time.Instant start)
        Returns elapsed time between given start and current time as a Duration.
      • earlier

        public static java.time.Instant earlier​(java.time.Instant time1,
                                                java.time.Instant time2)
        Determines the earlier of two instants.
      • later

        public static java.time.Instant later​(java.time.Instant time1,
                                              java.time.Instant time2)
        Determines the later of two instants.
      • longest

        public static java.time.Duration longest​(java.time.Duration duration1,
                                                 java.time.Duration duration2)
        Determines the longest of two durations.
      • dateTimeFormat

        public static java.lang.String dateTimeFormat​(java.time.Instant time)
        Formats a given date+time value to an ISO 8601 string.
        Parameters:
        time - date and time to format