Package adams.core

Class SecondFormat

  • All Implemented Interfaces:
    Serializable

    public class SecondFormat
    extends Object
    implements Serializable
    Formatting class for turning byte amounts into kb, mb, etc.

    Format: {t|T}[.N]{s|m|h|d|w}

    • b|B
      "t" outputs the amount without thousand separators, "T" includes them.
    • .N
      Prints "N" decimal places
    • S|M|H|D|W
      Specifies the unit to use: s=seconds, m=minutes, h=hours, d=days, w=weeks
      Lower case does not add a specifier like "ms", upper case does.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    See Also:
    Serialized Form
    • Field Detail

      • m_Format

        protected String m_Format
        the format string.
      • m_UseThousandSeparators

        protected boolean m_UseThousandSeparators
        whether to use thousand separators or not.
      • m_NumDecimals

        protected int m_NumDecimals
        the number of decimals to use.
      • m_AddUnit

        protected boolean m_AddUnit
        whether to add a unit specifier at the end.
    • Constructor Detail

      • SecondFormat

        public SecondFormat​(String format)
        Initializes the formatter.
        Parameters:
        format - the format to use
    • Method Detail

      • parseFormat

        protected boolean parseFormat​(String format,
                                      boolean justTest)
        Parses the format.
        Parameters:
        format - the format to parse
        justTest - if true then parameters derived won't be set
      • isValid

        public boolean isValid​(String format)
        Tests the format.
        Parameters:
        format - the format to test
        Returns:
        true if valid
      • getFormat

        public String getFormat()
        Returns the format being used.
        Returns:
        the format, null if invalid one provided
      • format

        public String format​(long seconds)
        Formats the given second amount according to the format.
        Parameters:
        seconds - the amount to format
        Returns:
        the formatted amount
      • format

        public String format​(double seconds)
        Formats the given second amount according to the format.
        Parameters:
        seconds - the amount to format
        Returns:
        the formatted amount
      • toString

        public String toString()
        Returns a short string description.
        Overrides:
        toString in class Object
        Returns:
        the format string
      • insertThousandSeparators

        public static String insertThousandSeparators​(String number)
        Inserts thousand separators into the number string (integer or float).
        Parameters:
        number - the string to inject with separators
        Returns:
        the processed string
      • toMinutes

        public static String toMinutes​(double seconds,
                                       int decimals)
        Turns the number of seconds into minutes.
        Parameters:
        seconds - the number of seconds to format
        decimals - the number of decimals after the decimal point
        Returns:
        the minute string
      • toHours

        public static String toHours​(double seconds,
                                     int decimals)
        Turns the number of seconds into hours.
        Parameters:
        seconds - the number of seconds to format
        decimals - the number of decimals after the decimal point
        Returns:
        the hour string
      • toDays

        public static String toDays​(double seconds,
                                    int decimals)
        Turns the number of seconds into days.
        Parameters:
        seconds - the number of seconds to format
        decimals - the number of decimals after the decimal point
        Returns:
        the day string
      • toWeeks

        public static String toWeeks​(double seconds,
                                     int decimals)
        Turns the number of seconds into weeks.
        Parameters:
        seconds - the number of seconds to format
        decimals - the number of decimals after the decimal point
        Returns:
        the week string
      • toBestFit

        public static String toBestFit​(double seconds,
                                       int decimals)
        Turns the number of seconds in the shortest representation (sec, min, ...).
        Parameters:
        seconds - the seconds to convert
        decimals - the number of decimals after the decimal point
        Returns:
        the optimal number string
      • toMixed

        public static String toMixed​(double seconds)
        Turns the number of seconds into a mixed representation (1w 2d 3h 4m 5s).
        Parameters:
        seconds - the seconds to convert
        Returns:
        the mixed string