I am trying to convert strings like 1h 30m 5s
or 5m
or 38s
or 1h 3s
into an integer value representing total time in seconds. So for example, 1m 20s
would result in an integer value of 80 for 80 seconds.
I am using Joda Time:
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendDays().appendSuffix("d").appendSeparatorIfFieldsAfter(" ")
.appendHours().appendSuffix("h").appendSeparatorIfFieldsAfter(" ")
.appendMinutes().appendSuffix("m").appendSeparatorIfFieldsAfter(" ")
.appendSeconds().appendSuffix("s")
.toFormatter();
int time = formatter.parsePeriod("30s").getSeconds(); //Throws IllegalArgumentException
Throws an IllegalArgumentException with practically every string i pass in, saying "Invalid format".
I don't understand why this wouldn't be working. I can't pass ANYTHING into this without getting an illegalArgumentException. Does anybody have any guidance on how to tweak my formatter settings to achieve my desired result?