Class FormatUtils

java.lang.Object
org.apache.nifi.util.FormatUtils

public class FormatUtils extends Object
  • Field Details

    • TIME_DURATION_REGEX

      public static final String TIME_DURATION_REGEX
    • TIME_DURATION_PATTERN

      public static final Pattern TIME_DURATION_PATTERN
    • EPOCH_INITIAL_DATE

      private static final LocalDate EPOCH_INITIAL_DATE
  • Constructor Details

    • FormatUtils

      public FormatUtils()
  • Method Details

    • formatCount

      public static String formatCount(long count)
      Formats the specified count by adding commas.
      Parameters:
      count - the value to add commas to
      Returns:
      the string representation of the given value with commas included
    • formatMinutesSeconds

      public static String formatMinutesSeconds(long sourceDuration, TimeUnit sourceUnit)
      Formats the specified duration in 'mm:ss.SSS' format.
      Parameters:
      sourceDuration - the duration to format
      sourceUnit - the unit to interpret the duration
      Returns:
      representation of the given time data in minutes/seconds
    • formatHoursMinutesSeconds

      public static String formatHoursMinutesSeconds(long sourceDuration, TimeUnit sourceUnit)
      Formats the specified duration in 'HH:mm:ss.SSS' format.
      Parameters:
      sourceDuration - the duration to format
      sourceUnit - the unit to interpret the duration
      Returns:
      representation of the given time data in hours/minutes/seconds
    • pad2Places

      private static String pad2Places(long val)
    • pad3Places

      private static String pad3Places(long val)
    • formatDataSize

      public static String formatDataSize(double dataSize)
      Formats the specified data size in human readable format.
      Parameters:
      dataSize - Data size in bytes
      Returns:
      Human readable format
    • getTimeDuration

      public static long getTimeDuration(String value, TimeUnit desiredUnit)
      Returns a time duration in the requested TimeUnit after parsing the String input. If the resulting value is a decimal (i.e. 25 hours -> TimeUnit.DAYS = 1.04), the value is rounded. Use getPreciseTimeDuration(String, TimeUnit) if fractional values are desirable
      Parameters:
      value - the raw String input (i.e. "28 minutes")
      desiredUnit - the requested output TimeUnit
      Returns:
      the whole number value of this duration in the requested units
      See Also:
    • getPreciseTimeDuration

      public static double getPreciseTimeDuration(String value, TimeUnit desiredUnit)
      Returns the parsed and converted input in the requested units.

      If the value is 0 <= x < 1 in the provided units, the units will first be converted to a smaller unit to get a value >= 1 (i.e. 0.5 seconds -> 500 milliseconds). This is because the underlying unit conversion cannot handle decimal values.

      If the value is x >= 1 but x is not a whole number, the units will first be converted to a smaller unit to attempt to get a whole number value (i.e. 1.5 seconds -> 1500 milliseconds).

      If the value is x < 1000 and the units are TimeUnit.NANOSECONDS, the result will be a whole number of nanoseconds, rounded (i.e. 123.4 ns -> 123 ns).

      This method handles decimal values over 1 ns, but < 1 ns will return 0 in any other unit.

      Examples:

      "10 seconds", TimeUnit.MILLISECONDS -> 10_000.0 "0.010 s", TimeUnit.MILLISECONDS -> 10.0 "0.010 s", TimeUnit.SECONDS -> 0.010 "0.010 ns", TimeUnit.NANOSECONDS -> 1 "0.010 ns", TimeUnit.MICROSECONDS -> 0

      Parameters:
      value - the String input
      desiredUnit - the desired output TimeUnit
      Returns:
      the parsed and converted amount (without a unit)
    • formatUtilization

      public static String formatUtilization(double utilization)
    • prepareLenientCaseInsensitiveDateTimeFormatter

      public static DateTimeFormatter prepareLenientCaseInsensitiveDateTimeFormatter(String pattern)
    • formatNanos

      public static String formatNanos(long nanos, boolean includeTotalNanos)
      Formats nanoseconds in the format: 3 seconds, 8 millis, 3 nanos - if includeTotalNanos = false, 3 seconds, 8 millis, 3 nanos (3008000003 nanos) - if includeTotalNanos = true
      Parameters:
      nanos - the number of nanoseconds to format
      includeTotalNanos - whether or not to include the total number of nanoseconds in parentheses in the returned value
      Returns:
      a human-readable String that is a formatted representation of the given number of nanoseconds.
    • parseToInstant

      public static Instant parseToInstant(DateTimeFormatter formatter, String text)
      Parse text to Instant - support different formats like: zoned date time, date time, date, time
      Parameters:
      formatter - configured formatter
      text - text which will be parsed
      Returns:
      parsed Instant
    • toInstantInSystemDefaultTimeZone

      private static Instant toInstantInSystemDefaultTimeZone(LocalDateTime dateTime)