Class TimeUtils


  • public class TimeUtils
    extends Object
    • Field Detail

      • VALID_MIN_TIME_MILLIS

        public static final long VALID_MIN_TIME_MILLIS
      • VALID_MAX_TIME_MILLIS

        public static final long VALID_MAX_TIME_MILLIS
      • VALID_TIME_INTERVAL

        public static final org.joda.time.Interval VALID_TIME_INTERVAL
    • Method Detail

      • timeUnitFromString

        @Nullable
        public static TimeUnit timeUnitFromString​(@Nullable
                                                  String timeUnitString)
        Converts a time unit string into TimeUnit, ignoring case. For null or empty time unit string, returns null.

        Besides the standard time unit, also support the following time unit strings:

        • "daysSinceEpoch" -> DAYS
        • "hoursSinceEpoch" -> HOURS
        • "minutesSinceEpoch" -> MINUTES
        • "secondsSinceEpoch" -> SECONDS
        • "millisSinceEpoch"/"millisecondsSinceEpoch" -> MILLISECONDS
        • "microsSinceEpoch"/"microsecondsSinceEpoch" -> MICROSECONDS
        • "nanosSinceEpoch"/"nanosecondsSinceEpoch" -> NANOSECONDS
        Parameters:
        timeUnitString - The time unit string to convert, e.g. "DAYS" or "SECONDS"
        Returns:
        The corresponding TimeUnit
      • timeValueInValidRange

        public static boolean timeValueInValidRange​(long timeValueInMillis)
        Given a time value, returns true if the value is between a valid range, false otherwise.

        The current valid range used is between beginning of 1971 and beginning of 2071.

      • isValidTimeInterval

        public static boolean isValidTimeInterval​(org.joda.time.Interval timeInterval)
        Given a time interval, returns true if the interval is between a valid range, false otherwise.

        The current valid range used is between beginning of 1971 and beginning of 2071.

      • getValidMinTimeMillis

        public static long getValidMinTimeMillis()
        Returns the minimum valid time in milliseconds.
      • getValidMaxTimeMillis

        public static long getValidMaxTimeMillis()
        Returns the maximum valid time in milliseconds.
      • convertPeriodToMillis

        public static Long convertPeriodToMillis​(String timeStr)
        Converts a string representing a period/duration to corresponding milliseconds. For ex, input "1d" would return 86400000L. Supported units are days (d), hours (h), minutes (m) and seconds (s).
        Parameters:
        timeStr - string representing the duration
        Returns:
        the corresponding time converted to milliseconds
        Throws:
        IllegalArgumentException - if the string does not conform to the expected format
      • convertTimestampToMillis

        public static Long convertTimestampToMillis​(String timeStr)
        Converts a string representing the timestamp to corresponding milliseconds.
        Parameters:
        timeStr - string representing the timestamp in ISO 8601 format
        Returns:
        the corresponding time converted to milliseconds
        Throws:
        IllegalArgumentException - if the string does not conform to the expected format
      • convertMillisToPeriod

        public static String convertMillisToPeriod​(Long millis)
        Converts milliseconds into human readable duration string. For ex, input of 86400000L would return "1d".
        Parameters:
        millis - the value in milliseconds to be converted
        Returns:
        the corresponding human readable string or empty string if value cannot be converted
      • isPeriodValid

        public static boolean isPeriodValid​(String timeStr)
        Checks if the given period string is valid i.e. has format like 30h, 15d, 10m
      • isTimestampValid

        public static boolean isTimestampValid​(String timeStr)
        Checks if the given timestamp string is valid