public static long parseDuration ( CharSequence duration ) { // Match the duration against the regular expression. Matcher matcher = DURATION_PATTERN . matcher ( duration ) ; // Check that the argument is of the right format accepted by this method. if ( ! matcher . matches ( ) ) { throw new IllegalArgumentException ( "The duration definition is not in the correct format." ) ; } // This accumulates the duration. long result = 0 ; int numGroups = matcher . groupCount ( ) ; // Extract the days. if ( numGroups >= 1 ) { String daysString = matcher . group ( 1 ) ; result += ( daysString == null ) ? 0 : ( Long . parseLong ( daysString . substring ( 0 , daysString . length ( ) - 1 ) ) * 24 * 60 * 60 * 1000 ) ; } // Extract the hours. if ( numGroups >= 2 ) { String hoursString = matcher . group ( 2 ) ; result += ( hoursString == null ) ? 0 : ( Long . parseLong ( hoursString . substring ( 0 , hoursString . length ( ) - 1 ) ) * 60 * 60 * 1000 ) ; } // Extract the minutes. if ( numGroups >= 3 ) { String minutesString = matcher . group ( 3 ) ; result += ( minutesString == null ) ? 0 : ( Long . parseLong ( minutesString . substring ( 0 , minutesString . length ( ) - 1 ) ) * 60 * 1000 ) ; } // Extract the seconds. if ( numGroups >= 4 ) { String secondsString = matcher . group ( 4 ) ; result += ( secondsString == null ) ? 0 : ( Long . parseLong ( secondsString . substring ( 0 , secondsString . length ( ) - 1 ) ) * 1000 ) ; } return result ; }
Parses a duration defined as a string giving a duration in days hours minutes and seconds into a number of milliseconds equal to that duration .