static public TimePeriod from ( String quantityAndUnit ) { String trimed = quantityAndUnit . trim ( ) ; String allExceptLast = trimed . substring ( 0 , trimed . length ( ) - 1 ) ; if ( StringUtils . isNumericSpace ( allExceptLast ) ) { // short format long quantity = Long . parseLong ( allExceptLast . trim ( ) ) ; TimePeriodUnit unit = TimePeriodUnit . from ( Character . toUpperCase ( trimed . charAt ( trimed . length ( ) - 1 ) ) ) ; return new TimePeriod ( quantity , unit ) ; } else { String [ ] durationAndUnit = StringUtils . split ( trimed ) ; Long duration = Long . valueOf ( durationAndUnit [ 0 ] ) ; TimePeriodUnit unit = TimePeriodUnit . from ( durationAndUnit [ 1 ] ) ; return new TimePeriod ( duration , unit ) ; } }
Parse strings like 1 hour 2 days 3 Years 12 minute into TimePeriod . Short formats like 1H 2 D 3y are also supported .