首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >“PT”前缀的持续时间是什么?

“PT”前缀的持续时间是什么?
EN

Stack Overflow用户
提问于 2018-07-04 15:23:59
回答 3查看 37.3K关注 0票数 48

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

"PT10S“表示10秒,接受"10秒”有什么问题?!好吧,不要紧。

我只是好奇为什么选择PT前缀(而不是"DU“,例如)为什么在这里有前缀总比没有前缀好?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-07-04 15:27:22

可以在Jesper链接到的页面上找到(ISO-8601 - Data elements and interchange formats – Information interchange – Representation of dates and times)

代码语言:javascript
运行
复制
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的意思是“周期”,因为没有日期部分,所以它只有一个“时间”。

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

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

票数 67
EN

Stack Overflow用户

发布于 2018-07-04 17:13:43

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

选择period的

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

票数 10
EN

Stack Overflow用户

发布于 2020-08-06 16:38:35

代码语言:javascript
运行
复制
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);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51168022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档