public static BigDecimal durationNs ( String durationP ) { if ( durationP == null ) { return null ; } String duration = durationP . trim ( ) ; if ( duration . length ( ) == 0 ) { return null ; } int unitPos = 1 ; while ( unitPos < duration . length ( ) && ( Character . isDigit ( duration . charAt ( unitPos ) ) || duration . charAt ( unitPos ) == ' ' ) ) { unitPos ++ ; } if ( unitPos >= duration . length ( ) ) { throw new IllegalArgumentException ( "Time unit not found in string: " + duration ) ; } String tail = duration . substring ( unitPos ) ; Long multiplier = null ; Integer unitEnd = null ; for ( int i = 0 ; i < TIME_UNITS . length ; i ++ ) { if ( tail . startsWith ( TIME_UNITS [ i ] ) ) { multiplier = UNIT_MULTIPLIERS [ i ] ; unitEnd = unitPos + TIME_UNITS [ i ] . length ( ) ; break ; } } if ( multiplier == null ) { throw new IllegalArgumentException ( "Unknown time unit in string: " + duration ) ; } BigDecimal value = new BigDecimal ( duration . substring ( 0 , unitPos ) ) ; value = value . multiply ( BigDecimal . valueOf ( multiplier ) ) ; String remaining = duration . substring ( unitEnd ) ; BigDecimal remainingValue = durationNs ( remaining ) ; if ( remainingValue != null ) { value = value . add ( remainingValue ) ; } return value ; }
Parses a duration string anr returns its value in nanoseconds .