| Modifier and Type | Method and Description |
|---|---|
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.
|
static double |
convertAndRound(long sourceDuration,
TimeUnit sourceTimeUnit,
TimeUnit targetTimeUnit)
Converts the given duration to a different time unit, with double-precision.
|
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.
|
static ChronoUnit |
toChronoUnit(TimeUnit timeUnit)
Converts a given
TimeUnit to the equivalent ChronoUnit. |
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()
sourceDuration - the time duration to be convertedsourceTimeUnit - the unit of the sourceDuration argument, not nulltargetTimeUnit - the target time unit, not nullNullPointerException - if any of the specified time units is nullpublic 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:
decimalPlaces is greater than zero, the number is rounded to the
specified number of decimal placesdecimalPlaces is zero, the number is rounded to the nearest integerdecimalPlaces is 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
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 roundedNullPointerException - if any of the specified time units is nullpublic 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);
sourceDuration - the time duration to be converted, not nullsourceTimeUnit - the unit of the sourceDuration argument, not nulltargetTimeUnit - the target time unitNullPointerException - if any of the specified time units is nullpublic static ChronoUnit toChronoUnit(TimeUnit timeUnit)
TimeUnit to the equivalent ChronoUnit.timeUnit - the TimeUnit to be convertedChronoUnitCopyright © 2023. All rights reserved.