“ PT”前缀在持续时间中代表什么?

时间:2018-07-04 07:23:59

标签: java duration java-time iso8601 period

我正在尝试使用Duration类而不是long。 它具有出色的文字语法。我喜欢它的灵活性,尽管看起来很奇怪。

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

我很好奇为什么选择了PT前缀(例如不是“ DU”),为什么这里的任何前缀总比没有好?

3 个答案:

答案 0 :(得分:12)

在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.

所以PT表示时间段。

选择该“原因”的原因是,您必须询问编写该标准的ISO成员,但我猜想它很容易解析。 (简短而明确)

答案 1 :(得分:4)

Java在一段时间内采用了ISO 8601标准格式的子集。因此,“为什么”是标准如此编写的原因,这是一种猜测游戏。我去的是:

    选择了
  • P作为期间,以便您可以将持续时间与日期和/或时间区分开。特别是由于句点也可能以与本地日期时间相同的格式写成,例如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的时间。
    • 要消除本来就模棱两可的M几个月或几分钟的歧义:P3M明确意味着3个月,而PT3M则意味着3分钟。

答案 2 :(得分:0)

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);
    }
}