Class FormatUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final double
private static final double
private static final double
private static final double
private static final String
private static final LocalDate
private static final String
private static final String
private static final String
private static final String
private static final String
static final Pattern
static final String
private static final String
private static final String
private static final String
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static long
calculateMultiplier
(TimeUnit originalTimeUnit, TimeUnit newTimeUnit) Returns the numerical multiplier to convert a value fromoriginalTimeUnit
tonewTimeUnit
(i.e.protected static TimeUnit
determineTimeUnit
(String rawUnit) Returns theTimeUnit
enum that maps to the provided rawString
input.static String
formatCount
(long count) Formats the specified count by adding commas.static String
formatDataSize
(double dataSize) Formats the specified data size in human readable format.static String
formatHoursMinutesSeconds
(long sourceDuration, TimeUnit sourceUnit) Formats the specified duration in 'HH:mm:ss.SSS' format.static String
formatMinutesSeconds
(long sourceDuration, TimeUnit sourceUnit) Formats the specified duration in 'mm:ss.SSS' format.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 = truestatic String
formatUtilization
(double utilization) static double
getPreciseTimeDuration
(String value, TimeUnit desiredUnit) Returns the parsed and converted input in the requested units.protected static TimeUnit
getSmallerTimeUnit
(TimeUnit originalUnit) Returns the next smallestTimeUnit
(i.e.static long
getTimeDuration
(String value, TimeUnit desiredUnit) Returns a time duration in the requestedTimeUnit
after parsing theString
input.protected static boolean
Returnstrue
if this raw unitString
is parsed as representing "weeks", which does not have a value in theTimeUnit
enum.private static String
makeWholeNumberTime
(double decimal, TimeUnit timeUnit) Converts the provided time duration value to one that can be represented as a whole number.private static String
pad2Places
(long val) private static String
pad3Places
(long val) static Instant
parseToInstant
(DateTimeFormatter formatter, String text) Parse text to Instant - support different formats like: zoned date time, date time, date, time (similar to those supported in SimpleDateFormat)static DateTimeFormatter
private static Instant
-
Field Details
-
UNION
- See Also:
-
BYTES_IN_KILOBYTE
private static final double BYTES_IN_KILOBYTE- See Also:
-
BYTES_IN_MEGABYTE
private static final double BYTES_IN_MEGABYTE- See Also:
-
BYTES_IN_GIGABYTE
private static final double BYTES_IN_GIGABYTE- See Also:
-
BYTES_IN_TERABYTE
private static final double BYTES_IN_TERABYTE- See Also:
-
NANOS
-
MILLIS
-
SECS
-
MINS
-
HOURS
-
DAYS
-
WEEKS
-
VALID_TIME_UNITS
-
TIME_DURATION_REGEX
-
TIME_DURATION_PATTERN
-
TIME_UNIT_MULTIPLIERS
-
EPOCH_INITIAL_DATE
-
-
Constructor Details
-
FormatUtils
public FormatUtils()
-
-
Method Details
-
formatCount
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
Formats the specified duration in 'mm:ss.SSS' format.- Parameters:
sourceDuration
- the duration to formatsourceUnit
- the unit to interpret the duration- Returns:
- representation of the given time data in minutes/seconds
-
formatHoursMinutesSeconds
Formats the specified duration in 'HH:mm:ss.SSS' format.- Parameters:
sourceDuration
- the duration to formatsourceUnit
- the unit to interpret the duration- Returns:
- representation of the given time data in hours/minutes/seconds
-
pad2Places
-
pad3Places
-
formatDataSize
Formats the specified data size in human readable format.- Parameters:
dataSize
- Data size in bytes- Returns:
- Human readable format
-
getTimeDuration
Returns a time duration in the requestedTimeUnit
after parsing theString
input. If the resulting value is a decimal (i.e.25 hours -> TimeUnit.DAYS = 1.04
), the value is rounded. UsegetPreciseTimeDuration(String, TimeUnit)
if fractional values are desirable- Parameters:
value
- the raw String input (i.e. "28 minutes")desiredUnit
- the requested outputTimeUnit
- Returns:
- the whole number value of this duration in the requested units
- See Also:
-
getPreciseTimeDuration
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 areTimeUnit.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 return0
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
- theString
inputdesiredUnit
- the desired outputTimeUnit
- Returns:
- the parsed and converted amount (without a unit)
-
makeWholeNumberTime
Converts the provided time duration value to one that can be represented as a whole number. Returns aList
containing the new value as along
at index 0 and theTimeUnit
at index 1. If the incoming value is already whole, it is returned as is. If the incoming value cannot be made whole, a whole approximation is returned. For values>= 1 TimeUnit.NANOSECONDS
, the value is rounded (i.e. 123.4 ns -> 123 ns). For values< 1 TimeUnit.NANOSECONDS
, the constant [1L,TimeUnit.NANOSECONDS
] is returned as the smallest measurable unit of time.Examples:
1,
TimeUnit.SECONDS
-> [1,TimeUnit.SECONDS
] 1.1,TimeUnit.SECONDS
-> [1100,TimeUnit.MILLISECONDS
] 0.1,TimeUnit.SECONDS
-> [100,TimeUnit.MILLISECONDS
] 0.1,TimeUnit.NANOSECONDS
-> [1,TimeUnit.NANOSECONDS
]- Parameters:
decimal
- the time duration as a decimaltimeUnit
- the current time unit- Returns:
- the time duration as a whole number (
long
) and the smaller time unit used
-
calculateMultiplier
Returns the numerical multiplier to convert a value fromoriginalTimeUnit
tonewTimeUnit
(i.e. forTimeUnit.DAYS -> TimeUnit.MINUTES
would return 24 * 60 = 1440). If the original and new units are the same, returns 1. If the new unit is larger than the original (i.e. the result would be less than 1), throws anIllegalArgumentException
.- Parameters:
originalTimeUnit
- the source time unitnewTimeUnit
- the destination time unit- Returns:
- the numerical multiplier between the units
-
getSmallerTimeUnit
Returns the next smallestTimeUnit
(i.e.TimeUnit.DAYS -> TimeUnit.HOURS
). If the parameter isnull
orTimeUnit.NANOSECONDS
, anIllegalArgumentException
is thrown because there is no valid smaller TimeUnit.- Parameters:
originalUnit
- the TimeUnit- Returns:
- the next smaller TimeUnit
-
isWeek
Returnstrue
if this raw unitString
is parsed as representing "weeks", which does not have a value in theTimeUnit
enum.- Parameters:
rawUnit
- the String containing the desired unit- Returns:
- true if the unit is "weeks"; false otherwise
-
determineTimeUnit
Returns theTimeUnit
enum that maps to the provided rawString
input. The highest time unit isTimeUnit.DAYS
. Any input that cannot be parsed will result in anIllegalArgumentException
.- Parameters:
rawUnit
- the String to parse- Returns:
- the TimeUnit
-
formatUtilization
-
join
-
formatNanos
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 formatincludeTotalNanos
- 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.
-
prepareLenientCaseInsensitiveDateTimeFormatter
-
parseToInstant
Parse text to Instant - support different formats like: zoned date time, date time, date, time (similar to those supported in SimpleDateFormat)- Parameters:
formatter
- configured formattertext
- text which will be parsed- Returns:
- parsed Instant
-
toInstantInSystemDefaultTimeZone
-