Class TimeUnitConverter


  • public class TimeUnitConverter
    extends Object
    A utility class for TimeUnit conversion.
    Since:
    2.0.0
    Author:
    oswaldo.bapvic.jr
    • 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 converted
        sourceTimeUnit - the unit of the sourceDuration argument, not null
        targetTimeUnit - 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 decimalPlaces is greater than zero, the number is rounded to the specified number of decimal places
        • If decimalPlaces is zero, the number is rounded to the nearest integer
        • If decimalPlaces 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
         
        Parameters:
        sourceDuration - the time duration to be converted
        sourceTimeUnit - the unit of the sourceDuration argument, not null
        targetTimeUnit - the target time unit, not null
        decimalPlaces - 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
      • 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 null
        sourceTimeUnit - the unit of the sourceDuration argument, not null
        targetTimeUnit - the target time unit
        Returns:
        the converted duration, as double
        Throws:
        NullPointerException - if any of the specified time units is null