Created
August 20, 2014 20:17
-
-
Save ramseyboy/5f66dc33bfa77c07d58e to your computer and use it in GitHub Desktop.
Duration and Period
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
P is the duration designator (historically called "period") placed at the start of the duration representation. | |
Y is the year designator that follows the value for the number of years. | |
M is the month designator that follows the value for the number of months. | |
W is the week designator that follows the value for the number of weeks. | |
D is the day designator that follows the value for the number of days. | |
T is the time designator that precedes the time components of the representation. | |
H is the hour designator that follows the value for the number of hours. | |
M is the minute designator that follows the value for the number of minutes. | |
S is the second designator that follows the value for the number of seconds. | |
*/ | |
//"timeLeft": "P2DT12H47M37S", | |
public static Duration parseDuration(String duration) { | |
PeriodFormatter parser = ISOPeriodFormat.standard(); | |
MutablePeriod period = new MutablePeriod(); | |
parser.parseInto(period, duration, 0); | |
return period.toDurationFrom((new DateTime(0))); | |
} | |
public static void main(String... args) { | |
Period period = parseDuration("PT21H52M6S").toPeriod(); | |
PeriodFormatter minutesAndSeconds = new PeriodFormatterBuilder() | |
.appendHours() | |
.appendSeparatorIfFieldsBefore(" Hours ") | |
.appendMinutes() | |
.appendSeparatorIfFieldsBefore(" Minutes ") | |
.appendSeconds() | |
.appendSeparatorIfFieldsBefore(" Seconds ") | |
.toFormatter(); | |
String result = minutesAndSeconds.print(period); | |
out.println(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment