Java 什么是';PT&x27;前缀表示持续时间?

Java 什么是';PT&x27;前缀表示持续时间?,java,duration,java-time,iso8601,period,Java,Duration,Java Time,Iso8601,Period,我正在尝试使用Duration类而不是long。 它具有优越的文字语法。我喜欢它的灵活性,尽管它看起来很奇怪 “PT10S”表示10秒,接受“10秒”有什么问题?! 好吧,没关系 我只是好奇为什么选择了PT前缀(例如不是“DU”),为什么这里的前缀比什么都好?可以在Jesper链接到()的页面上找到 所以p的意思是“期间”,因为没有日期成分,所以它只有一个“时间” 您可以将其解释为“一段时间” 选择“为什么”这个选项时,您必须询问编写该标准的ISO成员,但我猜这更容易解析。(简短而明确)Java

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

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


我只是好奇为什么选择了PT前缀(例如不是“DU”),为什么这里的前缀比什么都好?

可以在Jesper链接到()的页面上找到

所以p的意思是“期间”,因为没有日期成分,所以它只有一个“时间”

您可以将其解释为“一段时间”


选择“为什么”这个选项时,您必须询问编写该标准的ISO成员,但我猜这更容易解析。(简短而明确)

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

  • 已选择时段的
    P
    ,以便您可以将持续时间与日期和/或时间区分开来。特别是,由于周期也可能以与本地日期时间相同的格式写入,例如
    P0003-06-04T12:30:05
    持续3年6个月4天12小时30分5秒,因此可能需要使用
    P
    进行区分。
    P
    还提供了一点快速方便的验证,以防您碰巧在预期持续时间的位置传递了一个完全不同的字符串。是的,
    PT10S
    看起来很奇怪,但一旦你习惯了它,你会立即意识到它是一个持续时间,这是很实用的
  • T
    选择日期部分和时间部分之间的时间有两个原因:
    • 与在同一位置有
      T
      的日期时间字符串保持一致,例如2018年7月4日15:00时的
      2018-07-04T15:00
    • 要消除本不明确的
      M
      数月或数分钟的歧义:
      P3M
      明确表示3个月,而
      PT3M
      表示3分钟
事实上,如果从1.8开始使用Java@开发的持续时间API,那么它们已经使用了标准ISO 8601
使用java文档,如下所示:
/**
*将ISO 8601持续时间应用于{@link ZonedDateTime}。
*
*因为JDK为持续时间的不同部分定义了不同的类型
*在规范中,当要将完整持续时间应用于
*{@link ZonedDateTime}。请参阅{@link Period}和{@link Duration}。
*
*持续时间规范的所有基于日期的部分(年、月、日或周)都将被解析
*使用{@linkperiod#parse(CharSequence)}并添加到时间。其余部分(小时,
*分钟,秒)使用{@link Duration#parse(CharSequence)}解析并添加到时间中。
*
*@param time要应用偏移的分区日期时间
*@param offset ISO 8601持续时间格式中的偏移量
*@返回已应用偏移量的分区日期时间
*/
公共静态ZonedDateTime addOffset(ZonedDateTime,字符串偏移量){}
从模式为PnDTnHnMn.nS的文本字符串获取持续时间,其中
nD=天数,
nH=小时数,
nM=分钟数,
n、 nS=秒数,小数点可以是点或逗号。
T=必须在由nH、nM、n.n组成的零件之前使用
java作为应用程序的实现示例
导入java.time.Duration;
公共类解析示例{
公共静态void main(字符串…参数){
parse(“PT20S”);//T必须位于开始到结束时间部分
解析(“P2D”);//2天
parse(“-P2D”);//减去2天
解析(“P-2DT-20S”);//秒
解析(“PT20H”);//小时
解析(“PT220H”);
解析(“PT20M”);//M分钟
parse(“PT20.3S”);//秒可以是分数,如20.3
解析(“P4DT12H20M20.3S”);
解析(“P-4DT-12H-20M-20.3S”);
解析(“-P4DT12H20M20.3S”);
}
私有静态void解析(字符串模式){
Duration d=Duration.parse(模式);
System.out.println(“模式:%s=>%s%n”,模式,d);
}
}

这是持续时间/时段的标准符号。换句话说,
P
标记开始(大概是“时段”的缩写),而
T
将年-月-日部分与时-分-秒部分分开。对于没有任何年、月、日的值,在开始时以
PT
结束。不,PT不代表时间段。这只是一个没有日期成分的时段,只有时间。奇怪的是,在java中,Period和Duration前面都有这个P,尽管Duration不包含日期组件,只有time,因此对于Duration,您将始终使用PT,对于Period,您可以使用PYMWDTHMSThanks@ACV。你是说这是一个周期,因为没有YMWD,这意味着“只有时间成分的周期”,我自由地将其翻译为时间周期。我会更新我的答案,使之更精确。@RobAu你说得对。这并不重要。实际上,一段时间听起来更容易记住
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.
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);
    }
}