Package net.obvj.performetrics.util
Class TimeUnitConverter
- java.lang.Object
-
- net.obvj.performetrics.util.TimeUnitConverter
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static doubleconvert(long sourceDuration, TimeUnit sourceTimeUnit, TimeUnit targetTimeUnit)Converts the given duration and time unit into another time unit, as double, with no rounding.static doubleconvertAndRound(long sourceDuration, TimeUnit sourceTimeUnit, TimeUnit targetTimeUnit)Converts the given duration to a different time unit, with double-precision.static doubleconvertAndRound(long sourceDuration, TimeUnit sourceTimeUnit, TimeUnit targetTimeUnit, int decimalPlaces)Converts the given duration to a different time unit, with double-precision and a custom number of decimal places.
-
-
-
Method Detail
-
convertAndRound
public static double convertAndRound(long sourceDuration, TimeUnit sourceTimeUnit, TimeUnit targetTimeUnit)Converts the given duration to a different time unit, with double-precision.
For example, to convert 10 minutes to milliseconds, use:
TimeUnitConverter.convertAndRound(10, TimeUnit.MINUTES, TimeUnit.MILLISECONDS);
Note: The number of decimal places applied is determined by calling
ConfigurationHolder.getConfiguration().getScale()- Parameters:
sourceDuration- the time duration to be convertedsourceTimeUnit- the unit of the sourceDuration argument, not nulltargetTimeUnit- the target time unit, not null- Returns:
- the converted duration, as double.
- Throws:
NullPointerException- if any of the specified time units is null
-
convertAndRound
public static double convertAndRound(long sourceDuration, TimeUnit sourceTimeUnit, TimeUnit targetTimeUnit, int decimalPlaces)Converts the given duration to a different time unit, with double-precision and a custom number of decimal places.
For example, to convert 999 milliseconds to seconds, with a precision of 2 decimal places, use:
TimeUnitConverter.convertAndRound(999, TimeUnit.MILLISECONDS, TimeUnit.SECONDS, 2);
Remarks:
- If
decimalPlacesis greater than zero, the number is rounded to the specified number of decimal places - If
decimalPlacesis zero, the number is rounded to the nearest integer - If
decimalPlacesis less than zero, the number is rounded to the left of the decimal point
Examples:
convertAndRound(988, TimeUnit.MILLISECONDS, TimeUnit.SECONDS, 2) = 0.99 convertAndRound(988, TimeUnit.MILLISECONDS, TimeUnit.SECONDS, 0) = 1
- Parameters:
sourceDuration- the time duration to be convertedsourceTimeUnit- the unit of the sourceDuration argument, not nulltargetTimeUnit- the target time unit, not nulldecimalPlaces- the number of decimal places to which the number will be rounded- Returns:
- the converted duration, as double.
- Throws:
NullPointerException- if any of the specified time units is null
- If
-
convert
public static double convert(long sourceDuration, TimeUnit sourceTimeUnit, TimeUnit targetTimeUnit)Converts the given duration and time unit into another time unit, as double, with no rounding.
For example, to convert 10 minutes to milliseconds, use:
TimeUnitConverter.convert(10, TimeUnit.MINUTES, TimeUnit.MILLISECONDS);
- Parameters:
sourceDuration- the time duration to be converted, not nullsourceTimeUnit- the unit of the sourceDuration argument, not nulltargetTimeUnit- the target time unit- Returns:
- the converted duration, as double
- Throws:
NullPointerException- if any of the specified time units is null
-
-