What does ‘PT’ prefix stand for in Duration?

We Are Going To Discuss About What does ‘PT’ prefix stand for in Duration?. So lets Start this Java Article.

What does ‘PT’ prefix stand for in Duration?

Advertisements
  1. 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:

  2. 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:

Solution 1

Advertisements

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)

Original Author Rob Audenaerde Of This Content

Solution 2

Advertisements

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 example P0003-06-04T12:30:05 for 3 years 6 months 4 days 12 hours 30 minutes 5 seconds, the P can be necessary to distinguish. The P 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 example 2018-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 while PT3M means 3 minutes.

Original Author Ole V.V. Of This Content

Solution 3

Advertisements
Actually if go on Duration API developed in Java @since 1.8 , they have gone with standard ISO 8601

with java doc as below  :

/**
 * Applies an ISO 8601 Duration to a {@link ZonedDateTime}.
 *
 * <p>Since the JDK defined different types for the different parts of a Duration
 * specification, this utility method is needed when a full Duration is to be applied to a
 * {@link ZonedDateTime}. See {@link Period} and {@link Duration}.
 *
 * <p>All date-based parts of a Duration specification (Year, Month, Day or Week) are parsed
 * using {@link Period#parse(CharSequence)} and added to the time. The remaining parts (Hour,
 * Minute, Second) are parsed using {@link Duration#parse(CharSequence)} and added to the time.
 *
 * @param time   A zoned date time to apply the offset to
 * @param offset The offset in ISO 8601 Duration format
 * @return A zoned date time with the offset applied
 */
public static ZonedDateTime addOffset(ZonedDateTime time, String offset) { }

Obtains a Duration from a text string of pattern: PnDTnHnMn.nS, where
nD = number of days,
nH = number of hours,
nM = number of minutes,
n.nS = number of seconds, the decimal point may be either a dot or a comma.
T = must be used before the part consisting of nH, nM, n.nS


Example of implementation with java as 

import java.time.Duration;

public class ParseExample {

    public static void main(String... args) {
        parse("PT20S");//T must be at the beginning to time part
        parse("P2D");//2 day
        parse("-P2D");//minus 2 days
        parse("P-2DT-20S");//S for seconds
        parse("PT20H");//H for hours
        parse("PT220H");
        parse("PT20M");//M for minutes
        parse("PT20.3S");//second can be in fraction like 20.3
        parse("P4DT12H20M20.3S");
        parse("P-4DT-12H-20M-20.3S");
        parse("-P4DT12H20M20.3S");
    }

    private static void parse(String pattern) {
        Duration d = Duration.parse(pattern);
        System.out.println("Pattern: %s => %s%n", pattern, d);
    }
}

Original Author Sandeep Jain Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment