3 回答

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊
正如在Jesper所鏈接到的頁面上可以找到的(ISO-8601-數(shù)據(jù)元素和交換格式–信息交換–日期和時(shí)間的表示)
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',因?yàn)闆]有日期成分,所以它只有一個(gè)'Time'。
您可以將其解釋為“時(shí)間段”
選擇“為什么”的原因是,您必須詢問編寫該標(biāo)準(zhǔn)的ISO成員,但我猜想它很容易解析。(簡(jiǎn)短明確)

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊
Java在一段時(shí)間內(nèi)采用了ISO 8601標(biāo)準(zhǔn)格式的子集。因此,“為什么”是按原樣編寫標(biāo)準(zhǔn)的原因,這是一個(gè)猜謎游戲。我去:
P
選擇了“期間”,以便您可以將持續(xù)時(shí)間與日期和/或時(shí)間區(qū)分開。特別是因?yàn)橐部梢杂门c本地日期時(shí)間相同的格式來寫時(shí)間段,例如P0003-06-04T12:30:05
3年6個(gè)月4天12小時(shí)30分鐘5秒,所以P
可能需要區(qū)分。該P
還提供了一點(diǎn)點(diǎn),但如果你碰巧經(jīng)過一個(gè)完全不同的字符串在持續(xù)時(shí)間預(yù)期的地方驗(yàn)證快速,便捷位。是的,PT10S
看起來很奇怪,但是一旦您習(xí)慣了它,就可以立即將其識(shí)別為持續(xù)時(shí)間,這很實(shí)用。T
選擇日期部分和時(shí)間部分之間的時(shí)間有兩個(gè)原因:為了與
T
位于同一位置的日期時(shí)間字符串保持一致,例如2018-07-04T15:00
2018年7月4日15:00時(shí)。在
M
幾個(gè)月或幾分鐘內(nèi)消除本來就模棱兩可的內(nèi)容:P3M
明確表示3個(gè)月,而PT3M
表示3分鐘。

TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊
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);
}
}
添加回答
舉報(bào)