Class DurationFormatter


  • public final class DurationFormatter
    extends Object
    Formats a duration to a string. This class is thread-safe and immutable.
    Since:
    2.0.0
    Author:
    Pascal Essiembre
    • Constructor Detail

      • DurationFormatter

        public DurationFormatter()
        Creates a duration with the default locale and full words for duration units.
    • Method Detail

      • withLocale

        public DurationFormatter withLocale​(Locale locale)
        Creates a copy if this formatter with the given locale. Default locale is English.
        Parameters:
        locale - locale
        Returns:
        duration formatter copy
      • getLocale

        public Locale getLocale()
      • withUnitPrecision

        public DurationFormatter withUnitPrecision​(int unitPrecision)
        Creates a copy if this formatter with the given unit precision.
        Parameters:
        unitPrecision - unit precision
        Returns:
        duration formatter copy
      • getUnitPrecision

        public int getUnitPrecision()
      • withNumberFormat

        public DurationFormatter withNumberFormat​(NumberFormat numberFormat)
        Creates a copy if this formatter with the given number format. Default number format is RBDurationUnitFormatter.FULL.
        Parameters:
        numberFormat - number format
        Returns:
        duration formatter copy
      • withHighestUnit

        public DurationFormatter withHighestUnit​(DurationUnit highestUnit)
        Creates a copy if this formatter with the given highest unit.
        Parameters:
        highestUnit - highest unit
        Returns:
        duration formatter copy
      • withLowestUnit

        public DurationFormatter withLowestUnit​(DurationUnit lowestUnit)
        Creates a copy if this formatter with the given lowest unit.
        Parameters:
        lowestUnit - lowest unit
        Returns:
        duration formatter copy
      • withUnitFormatter

        public DurationFormatter withUnitFormatter​(IDurationUnitFormatter unitFormatter)
        Creates a copy if this formatter with the given unit formatter.
        Parameters:
        unitFormatter - unit formatter
        Returns:
        duration formatter copy
      • withInnerSeparator

        public DurationFormatter withInnerSeparator​(String innerSeparator)
        Creates a copy if this formatter with the specified separator text to be inserted between the numeric and textual values. For instance, specifying an hyphen ('-') for a ten seconds duration would result in: 10-seconds. Default is a single space character.
        Parameters:
        innerSeparator - inner separator
        Returns:
        duration formatter copy
      • getInnerSeparator

        public String getInnerSeparator()
      • withOuterSeparator

        public DurationFormatter withOuterSeparator​(String outerSeparator)
        Creates a copy if this formatter with the specified separator text to be inserted between each formatter units (number and text pairs). For instance, specifying ", " for two minutes and ten seconds duration would result in: 2 minutes, 10 seconds. Default is a single space character.
        Parameters:
        outerSeparator - outer separator
        Returns:
        duration formatter copy
      • getOuterSeparator

        public String getOuterSeparator()
      • withOuterLastSeparator

        public DurationFormatter withOuterLastSeparator​(String outerLastSeparator)
        Creates a copy if this formatter with the specified separator text to be inserted between the second to last and last formatter units (number and text pairs). For instance, specifying " and " for 1 hour, two minutes and ten seconds duration would result in: 1 hour 2 minutes and 10 seconds. Default is null (fallsback to using getOuterSeparator()).
        Parameters:
        outerLastSeparator - last outer separator
        Returns:
        duration formatter copy
      • getOuterLastSeparator

        public String getOuterLastSeparator()
      • format

        public String format​(Duration duration)
        Formats the given duration to a string.
        Parameters:
        duration - the duration to format
        Returns:
        formatted duration
      • format

        public String format​(long duration)
        Formats the given duration (in milliseconds) to a string.
        Parameters:
        duration - the duration to format
        Returns:
        formatted duration
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object