What does 'PT' prefix stand for in Duration?
Java has taken a subset of the ISO 8601 standard format for a duration. So the “why” is why the standard was written the way it is, and it’s a guessing game. My go is:
P
for period was chosen so that you can distinguish a duration from a date and/or time. Especially since a period may also be written in the same format as a local date-time, for exampleP0003-06-04T12:30:05
for 3 years 6 months 4 days 12 hours 30 minutes 5 seconds, theP
can be necessary to distinguish. TheP
also gives a little but quick and convenient bit of validation in case you happen to pass a completely different string in a place where a duration was expected. And yes,PT10S
looks weird, but once you get accustomed to it, you recognize it immediately as a duration, which can be practical.T
for time between the date part and the time part was chosen for two reasons:- For consistency with date-time strings that have
T
in the same place, for example2018-07-04T15:00
for July 4, 2018 at 15:00 hours. - To disambiguate the otherwise ambiguous
M
for either months or minutes:P3M
unambiguously means 3 months whilePT3M
means 3 minutes.
- For consistency with date-time strings that have
As can be found on the page Jesper linked to (ISO-8601 - Data elements and interchange formats – Information interchange – Representation of dates and times)
P is the duration designator (for 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.
So P means 'Period' and because there are no date-components it only has a 'Time'.
You could interpret this as 'Period of Time'
The 'why' this was chosen, you have to ask the ISO members that wrote the standard, but my guess is that it is easier to parse. (short and unambigious)
The details for the time component are:
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.
The value of PT20S
then parses to:
- Period
- Time
- 20
- Seconds
So, a duration of 20 seconds.
More examples can be found in the javadoc: https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#parse-java.lang.CharSequence-