'PT' 前缀在 Duration 中代表什么?

What does 'PT' prefix stand for in Duration?

我正在尝试使用 Duration class 而不是 long。 它具有优越的文字语法。我喜欢它的灵活性,虽然它看起来很奇怪。

"PT10S"表示10秒,接受“10秒”有什么问题?! 好吧没关系。

我很好奇为什么选择了 PT 前缀(而不是 "DU",例如)以及为什么这里有前缀比没有前缀更好?

可以在 Jesper 链接到 (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.

所以 P 表示 'Period',因为没有日期组件,所以它只有 'Time'。

您可以将其解释为 'Period of Time'

'why'这个被选中了,你得去问写标准的ISO成员,不过我猜是这样更容易解析。 (简短而明确)

Java 在一段时间内采用了 ISO 8601 标准格式的子集。所以“为什么”就是为什么标准是这样写的,这是一个猜谜游戏。我的目标是:

    选择
  • P 表示时间段,以便您可以区分持续时间和日期 and/or 时间。特别是由于句点也可以用与本地日期时间相同的格式书写,例如 P0003-06-04T12:30:05 表示 3 年 6 个月 4 天 12 小时 30 分 5 秒,因此 P 可能是必要的区分。 P 还提供了一点点但快速方便的验证,以防您碰巧在预期持续时间的地方传递完全不同的字符串。是的,PT10S 看起来很奇怪,但是一旦你习惯了它,你会立即将它识别为一个持续时间,这很实用。
  • 选择日期部分和时间部分之间的时间
  • T 有两个原因:
    • 为了与在同一位置具有 T 的日期时间字符串保持一致,例如 2018-07-04T15:00 表示 2018 年 7 月 4 日 15:00 小时。
    • 为了消除月或分钟的歧义 MP3M 明确表示 3 个月,而 PT3M 表示 3 分钟。

实际上,如果继续 API 从 1.8 开始在 Java 中开发,他们已经采用了标准 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);
    }
}