猿问

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

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

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

我很好奇为什么选择了PT前缀(而不是“ DU”),为什么这里的前缀更好而不是没有?


达令说
浏览 1316回答 3
3回答

扬帆大鱼

正如在Jesper所链接到的页面上可以找到的(ISO-8601-数据元素和交换格式–信息交换–日期和时间的表示)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'。您可以将其解释为“时间段”选择“为什么”的原因是,您必须询问编写该标准的ISO成员,但我猜想它很容易解析。(简短明确)

Smart猫小萌

Java在一段时间内采用了ISO 8601标准格式的子集。因此,“为什么”是按原样编写标准的原因,这是一个猜谜游戏。我去:P选择了“期间”,以便您可以将持续时间与日期和/或时间区分开。特别是因为也可以用与本地日期时间相同的格式来写时间段,例如P0003-06-04T12:30:053年6个月4天12小时30分钟5秒,所以P可能需要区分。该P还提供了一点点,但如果你碰巧经过一个完全不同的字符串在持续时间预期的地方验证快速,便捷位。是的,PT10S看起来很奇怪,但是一旦您习惯了它,就可以立即将其识别为持续时间,这很实用。T 选择日期部分和时间部分之间的时间有两个原因:为了与T位于同一位置的日期时间字符串保持一致,例如2018-07-04T15:002018年7月4日15:00时。在M几个月或几分钟内消除本来就模棱两可的内容:P3M明确表示3个月,而PT3M表示3分钟。

慕娘9325324

Actually if go on Duration API developed in Java @since 1.8 , they have gone with standard ISO 8601with java doc as below&nbsp; :/**&nbsp;* Applies an ISO 8601 Duration to a {@link ZonedDateTime}.&nbsp;*&nbsp;* <p>Since the JDK defined different types for the different parts of a Duration&nbsp;* specification, this utility method is needed when a full Duration is to be applied to a&nbsp;* {@link ZonedDateTime}. See {@link Period} and {@link Duration}.&nbsp;*&nbsp;* <p>All date-based parts of a Duration specification (Year, Month, Day or Week) are parsed&nbsp;* using {@link Period#parse(CharSequence)} and added to the time. The remaining parts (Hour,&nbsp;* Minute, Second) are parsed using {@link Duration#parse(CharSequence)} and added to the time.&nbsp;*&nbsp;* @param time&nbsp; &nbsp;A zoned date time to apply the offset to&nbsp;* @param offset The offset in ISO 8601 Duration format&nbsp;* @return A zoned date time with the offset applied&nbsp;*/public static ZonedDateTime addOffset(ZonedDateTime time, String offset) { }Obtains a Duration from a text string of pattern: PnDTnHnMn.nS, wherenD = 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.nSExample of implementation with java as&nbsp;import java.time.Duration;public class ParseExample {&nbsp; &nbsp; public static void main(String... args) {&nbsp; &nbsp; &nbsp; &nbsp; parse("PT20S");//T must be at the beginning to time part&nbsp; &nbsp; &nbsp; &nbsp; parse("P2D");//2 day&nbsp; &nbsp; &nbsp; &nbsp; parse("-P2D");//minus 2 days&nbsp; &nbsp; &nbsp; &nbsp; parse("P-2DT-20S");//S for seconds&nbsp; &nbsp; &nbsp; &nbsp; parse("PT20H");//H for hours&nbsp; &nbsp; &nbsp; &nbsp; parse("PT220H");&nbsp; &nbsp; &nbsp; &nbsp; parse("PT20M");//M for minutes&nbsp; &nbsp; &nbsp; &nbsp; parse("PT20.3S");//second can be in fraction like 20.3&nbsp; &nbsp; &nbsp; &nbsp; parse("P4DT12H20M20.3S");&nbsp; &nbsp; &nbsp; &nbsp; parse("P-4DT-12H-20M-20.3S");&nbsp; &nbsp; &nbsp; &nbsp; parse("-P4DT12H20M20.3S");&nbsp; &nbsp; }&nbsp; &nbsp; private static void parse(String pattern) {&nbsp; &nbsp; &nbsp; &nbsp; Duration d = Duration.parse(pattern);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Pattern: %s => %s%n", pattern, d);&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答