3 回答
TA贡献1799条经验 获得超9个赞
正如在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成员,但我猜想它很容易解析。(简短明确)
TA贡献1911条经验 获得超7个赞
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分钟。
TA贡献1783条经验 获得超4个赞
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);
}
}
添加回答
举报