Class DurationFormatUtils

java.lang.Object
org.apache.commons.lang3.time.DurationFormatUtils

public class DurationFormatUtils extends Object

Duration formatting utilities and constants. The following table describes the tokens used in the pattern language for formatting.

Pattern Tokens
characterduration element
yyears
Mmonths
ddays
Hhours
mminutes
sseconds
Smilliseconds
'text'arbitrary text content
Note: It's not currently possible to include a single-quote in a format.
Token values are printed using decimal digits. A token character can be repeated to ensure that the field occupies a certain minimum size. Values will be left-padded with 0 unless padding is disabled in the method invocation.
Since:
2.1
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    (package private) static class 
    Element that is parsed from the format pattern.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) static final String
     
    (package private) static final String
     
    static final String
    Pattern used with FastDateFormat and SimpleDateFormat for the ISO 8601 period format used in durations.
    (package private) static final String
     
    (package private) static final String
     
    (package private) static final String
     
    (package private) static final String
     
    (package private) static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    DurationFormatUtils instances should NOT be constructed in standard programming.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) static String
    format(DurationFormatUtils.Token[] tokens, long years, long months, long days, long hours, long minutes, long seconds, long milliseconds, boolean padWithZeros)
    The internal method to do the formatting.
    static String
    formatDuration(long durationMillis, String format)
    Formats the time gap as a string, using the specified format, and padding with zeros.
    static String
    formatDuration(long durationMillis, String format, boolean padWithZeros)
    Formats the time gap as a string, using the specified format.
    static String
    formatDurationHMS(long durationMillis)
    Formats the time gap as a string.
    static String
    formatDurationISO(long durationMillis)
    Formats the time gap as a string.
    static String
    formatDurationWords(long durationMillis, boolean suppressLeadingZeroElements, boolean suppressTrailingZeroElements)
    Formats an elapsed time into a pluralization correct string.
    static String
    formatPeriod(long startMillis, long endMillis, String format)
    Formats the time gap as a string, using the specified format.
    static String
    formatPeriod(long startMillis, long endMillis, String format, boolean padWithZeros, TimeZone timezone)
    Formats the time gap as a string, using the specified format.
    static String
    formatPeriodISO(long startMillis, long endMillis)
    Formats the time gap as a string.
    (package private) static DurationFormatUtils.Token[]
    lexx(String format)
    Parses a classic date format string into Tokens
    private static String
    paddedValue(long value, boolean padWithZeros, int count)
    Converts a long to a String with optional zero padding.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • DurationFormatUtils

      public DurationFormatUtils()

      DurationFormatUtils instances should NOT be constructed in standard programming.

      This constructor is public to permit tools that require a JavaBean instance to operate.

  • Method Details

    • formatDurationHMS

      public static String formatDurationHMS(long durationMillis)

      Formats the time gap as a string.

      The format used is ISO 8601-like: HH:mm:ss.SSS.

      Parameters:
      durationMillis - the duration to format
      Returns:
      the formatted duration, not null
      Throws:
      IllegalArgumentException - if durationMillis is negative
    • formatDurationISO

      public static String formatDurationISO(long durationMillis)

      Formats the time gap as a string.

      The format used is the ISO 8601 period format.

      This method formats durations using the days and lower fields of the ISO format pattern, such as P7D6TH5M4.321S.

      Parameters:
      durationMillis - the duration to format
      Returns:
      the formatted duration, not null
      Throws:
      IllegalArgumentException - if durationMillis is negative
    • formatDuration

      public static String formatDuration(long durationMillis, String format)

      Formats the time gap as a string, using the specified format, and padding with zeros.

      This method formats durations using the days and lower fields of the format pattern. Months and larger are not used.

      Parameters:
      durationMillis - the duration to format
      format - the way in which to format the duration, not null
      Returns:
      the formatted duration, not null
      Throws:
      IllegalArgumentException - if durationMillis is negative
    • formatDuration

      public static String formatDuration(long durationMillis, String format, boolean padWithZeros)

      Formats the time gap as a string, using the specified format. Padding the left hand side of numbers with zeroes is optional.

      This method formats durations using the days and lower fields of the format pattern. Months and larger are not used.

      Parameters:
      durationMillis - the duration to format
      format - the way in which to format the duration, not null
      padWithZeros - whether to pad the left hand side of numbers with 0's
      Returns:
      the formatted duration, not null
      Throws:
      IllegalArgumentException - if durationMillis is negative
    • formatDurationWords

      public static String formatDurationWords(long durationMillis, boolean suppressLeadingZeroElements, boolean suppressTrailingZeroElements)

      Formats an elapsed time into a pluralization correct string.

      This method formats durations using the days and lower fields of the format pattern. Months and larger are not used.

      Parameters:
      durationMillis - the elapsed time to report in milliseconds
      suppressLeadingZeroElements - suppresses leading 0 elements
      suppressTrailingZeroElements - suppresses trailing 0 elements
      Returns:
      the formatted text in days/hours/minutes/seconds, not null
      Throws:
      IllegalArgumentException - if durationMillis is negative
    • formatPeriodISO

      public static String formatPeriodISO(long startMillis, long endMillis)

      Formats the time gap as a string.

      The format used is the ISO 8601 period format.

      Parameters:
      startMillis - the start of the duration to format
      endMillis - the end of the duration to format
      Returns:
      the formatted duration, not null
      Throws:
      IllegalArgumentException - if startMillis is greater than endMillis
    • formatPeriod

      public static String formatPeriod(long startMillis, long endMillis, String format)

      Formats the time gap as a string, using the specified format. Padding the left hand side of numbers with zeroes is optional.

      Parameters:
      startMillis - the start of the duration
      endMillis - the end of the duration
      format - the way in which to format the duration, not null
      Returns:
      the formatted duration, not null
      Throws:
      IllegalArgumentException - if startMillis is greater than endMillis
    • formatPeriod

      public static String formatPeriod(long startMillis, long endMillis, String format, boolean padWithZeros, TimeZone timezone)

      Formats the time gap as a string, using the specified format. Padding the left hand side of numbers with zeroes is optional and the time zone may be specified.

      When calculating the difference between months/days, it chooses to calculate months first. So when working out the number of months and days between January 15th and March 10th, it choose 1 month and 23 days gained by choosing January->February = 1 month and then calculating days forwards, and not the 1 month and 26 days gained by choosing March -> February = 1 month and then calculating days backwards.

      For more control, the Joda-Time library is recommended.

      Parameters:
      startMillis - the start of the duration
      endMillis - the end of the duration
      format - the way in which to format the duration, not null
      padWithZeros - whether to pad the left hand side of numbers with 0's
      timezone - the millis are defined in
      Returns:
      the formatted duration, not null
      Throws:
      IllegalArgumentException - if startMillis is greater than endMillis
    • format

      static String format(DurationFormatUtils.Token[] tokens, long years, long months, long days, long hours, long minutes, long seconds, long milliseconds, boolean padWithZeros)

      The internal method to do the formatting.

      Parameters:
      tokens - the tokens
      years - the number of years
      months - the number of months
      days - the number of days
      hours - the number of hours
      minutes - the number of minutes
      seconds - the number of seconds
      milliseconds - the number of millis
      padWithZeros - whether to pad
      Returns:
      the formatted string
    • paddedValue

      private static String paddedValue(long value, boolean padWithZeros, int count)

      Converts a long to a String with optional zero padding.

      Parameters:
      value - the value to convert
      padWithZeros - whether to pad with zeroes
      count - the size to pad to (ignored if padWithZeros is false)
      Returns:
      the string result
    • lexx

      static DurationFormatUtils.Token[] lexx(String format)
      Parses a classic date format string into Tokens
      Parameters:
      format - the format to parse, not null
      Returns:
      array of Token[]