Enum TimeFormat

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<TimeFormat>

    public enum TimeFormat
    extends java.lang.Enum<TimeFormat>
    This utility class provides methods to format and parse a timespan in a human readable form. The timespan is formatted broken down in days, hours, minutes, seconds, and milliseconds. The time span 5 minutes and 23 seconds is displayed simply as "5m23s" instead of the millisecond value "323000". A larger value might be displayed as "3d17h27m33s15ms".
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
      SINGLETON  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String formatDatetime​(long datetime)
      Format the given date in a human readable form.
      static java.lang.String formatDatetime​(java.util.Date date)
      Format the given date in a human readable form.
      static java.lang.String formatTimespan​(long millis)
      Format the given number of milliseconds in a more human readable form.
      static void formatTimespan​(long millis, java.lang.StringBuilder sb)
      Format the given number of milliseconds in a more human readable form and append it to the given StringBuilder.
      static java.util.Date parseDatetimeToDate​(java.lang.String s)
      Parse the given String date and time to a millisecond value.
      static long parseDatetimeToMillis​(java.lang.String s)
      Parse the given String date and time to a millisecond value.
      static long parseTimespanToMillis​(java.lang.String s)
      Parse the given String timespan to a millisecond value.
      static TimeFormat valueOf​(java.lang.String name)
      Returns the enum constant of this type with the specified name.
      static TimeFormat[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      • Methods inherited from class java.lang.Enum

        clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Enum Constant Detail

      • SINGLETON

        public static final TimeFormat SINGLETON
    • Method Detail

      • values

        public static TimeFormat[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (TimeFormat c : TimeFormat.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static TimeFormat valueOf​(java.lang.String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • parseDatetimeToMillis

        public static long parseDatetimeToMillis​(java.lang.String s)
                                          throws java.lang.IllegalArgumentException
        Parse the given String date and time to a millisecond value. The string must be in the form returned by formatDatetime(long) or formatDatetime(Date).
        Parameters:
        s - date time to parse - like "2012/04/05 14:38:03.311"
        Returns:
        the date and time, in milliseconds
        Throws:
        java.lang.IllegalArgumentException - if s is invalid
      • parseDatetimeToDate

        public static java.util.Date parseDatetimeToDate​(java.lang.String s)
                                                  throws java.lang.IllegalArgumentException
        Parse the given String date and time to a millisecond value. The string must be in the form returned by formatDatetime(long) or formatDatetime(Date).
        Parameters:
        s - date time to parse - like "2012/04/05 14:38:03.311"
        Returns:
        the date and time
        Throws:
        java.lang.IllegalArgumentException - if s is invalid
      • formatDatetime

        public static java.lang.String formatDatetime​(java.util.Date date)
        Format the given date in a human readable form.
        Parameters:
        date - the date
        Returns:
        a human readable date, such as "2012/04/05 14:38:03.311".
      • formatDatetime

        public static java.lang.String formatDatetime​(long datetime)
        Format the given date in a human readable form.
        Parameters:
        datetime - the date and time, in milliseconds
        Returns:
        a human readable date, such as "2012/04/05 14:38:03.311".
      • parseTimespanToMillis

        public static long parseTimespanToMillis​(java.lang.String s)
                                          throws java.lang.IllegalArgumentException
        Parse the given String timespan to a millisecond value. The string must be in the form returned by formatTimespan(long). In particular, zero time should be represented as "0ms", not as "0" or "".
        Parameters:
        s - timespan to parse - like 3d17h27m33s15ms
        Returns:
        the number of milliseconds
        Throws:
        java.lang.IllegalArgumentException - if the timespan is invalid
      • formatTimespan

        public static java.lang.String formatTimespan​(long millis)
        Format the given number of milliseconds in a more human readable form.
        Parameters:
        millis - number of milliseconds
        Returns:
        a human readable duration, such as "3d17h27m33s15ms".
      • formatTimespan

        public static void formatTimespan​(long millis,
                                          java.lang.StringBuilder sb)
        Format the given number of milliseconds in a more human readable form and append it to the given StringBuilder.
        Parameters:
        millis - number of milliseconds
        sb - append the formatted timestamp here