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();
}
Aggregations