Search in sources :

Example 16 with PeriodFormatterBuilder

use of org.joda.time.format.PeriodFormatterBuilder in project FlareBot by FlareBot.

the class GeneralUtils method parseTime.

/**
 * Parses time from string.
 * The input can be in these formats
 * s
 * m:s
 * h:m:s
 * XhXmXs
 * Examples:
 * 5 - Would be 5 seconds
 * 5s - Would also be 5 seconds
 * 1:10 - Would be 1 minute and 10 seconds
 * 1m10s - Would also be 1 minute and 10 seconds
 * 1:00:00 - Would be an hour
 * 1h - Would also be an hour
 *
 * @param time The time string to parse.
 * @return The long representing the time entered in millis from when this method was ran.
 */
public static Long parseTime(String time) {
    Matcher digitMatcher = timeRegex.matcher(time);
    if (digitMatcher.matches()) {
        try {
            return new PeriodFormatterBuilder().appendHours().appendSuffix(":").appendMinutes().appendSuffix(":").appendSeconds().toFormatter().parsePeriod(time).toStandardDuration().getMillis();
        } catch (IllegalArgumentException e) {
            return null;
        }
    }
    PeriodFormatter formatter = new PeriodFormatterBuilder().appendHours().appendSuffix("h").appendMinutes().appendSuffix("m").appendSeconds().appendSuffix("s").toFormatter();
    Period period;
    try {
        period = formatter.parsePeriod(time);
    } catch (IllegalArgumentException e) {
        return null;
    }
    return period.toStandardDuration().getMillis();
}
Also used : PeriodFormatterBuilder(org.joda.time.format.PeriodFormatterBuilder) PeriodFormatter(org.joda.time.format.PeriodFormatter) Matcher(java.util.regex.Matcher) Period(org.joda.time.Period)

Aggregations

PeriodFormatterBuilder (org.joda.time.format.PeriodFormatterBuilder)16 PeriodFormatter (org.joda.time.format.PeriodFormatter)15 Period (org.joda.time.Period)10 DateTime (org.joda.time.DateTime)6 Duration (org.joda.time.Duration)5 HashMap (java.util.HashMap)2 Map (java.util.Map)2 RuleSetBean (org.akaza.openclinica.domain.rule.RuleSetBean)2 ExpressionObjectWrapper (org.akaza.openclinica.domain.rule.expression.ExpressionObjectWrapper)2 ExpressionProcessor (org.akaza.openclinica.domain.rule.expression.ExpressionProcessor)2 PeriodParser (org.joda.time.format.PeriodParser)2 MalformedURLException (java.net.MalformedURLException)1 UnknownHostException (java.net.UnknownHostException)1 MessageFormat (java.text.MessageFormat)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 Matcher (java.util.regex.Matcher)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1