Class DurationFormatter


  • public final class DurationFormatter
    extends Object
    Formatter for a Duration value. Durations are split into avaialble ChronoUnits as specified in the DurationFormat, using unit Durations obtained from ChronoUnit values.

    The format is specified by a DurationFormat, allowing formats such as:

    • 1s 370ms
    • 1.37s
    • 2w 3d
    • 4w1d4h22m
    • 6 weeks, 4 days, 23 hours
    • -1m 30s
    Leading and trailing parts of the formatted representation which have a zero value are not shown. That is, a formatted value of 70 minutes with allowable units of seconds, minutes, hours, and days will not be "0d 1h 10m 0s"; instead, it would be formatted as "1h 10m".

    Zero Durations are always formatted with the unit specified in unitForZeroDuration.

    Unit suffix strings are obtained from a UnitSuffixProvider, which has access to the unit and the actual value being rendered, for the sake of maximum flexibility. UnitSuffixProviders has factory methods for more typical cases such as one suffix per unit, or one singular and one plural suffix per unit.

    Negative Duration values are split so that only the first part of the formatted value is formatted as a negative number; the smaller parts are formatted as positive numbers. See the example above: -90 seconds is formatted as -1 minutes, 30 seconds (not -1 minutes, -30 seconds).

    Note: Duration values are defined in terms of seconds and nanoseconds. Since calendars' day, month, and year lengths vary over time, this class uses the ChronoUnit.MONTHS and ChronoUnit.YEARS unit values that are approximations of actual calendar months and years.

    • Constructor Detail

      • DurationFormatter

        public DurationFormatter​(DurationFormat format)
        Create a DurationFormatter.
        Parameters:
        format - The formatting rules to use.
    • Method Detail

      • format

        public String format​(Duration duration)
        Format a Duration.
        Parameters:
        duration - The Duration to format.
        Returns:
        The formatted representation of the Duration.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object