我正在尝试使用Duration
类而不是long
。它具有优越的文字语法。我喜欢它的灵活性,尽管它看起来很奇怪。
"PT10S“表示10秒,接受"10秒”有什么问题?!好吧,不要紧。
我只是好奇为什么选择PT前缀(而不是"DU“,例如)为什么在这里有前缀总比没有前缀好?
发布于 2018-07-04 15:27:22
可以在Jesper链接到的页面上找到(ISO-8601 - Data elements and interchange formats – Information interchange – Representation of dates and times)
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成员,但我猜它更容易解析。(简短而明确)
发布于 2018-07-04 17:13:43
Java在一段时间内采用了ISO 8601标准格式的子集。所以“为什么”就是为什么标准是这样写的,这是一个猜谜游戏。我的目标是:
选择period的
P
是为了让您可以将持续时间与日期和/或时间区分开来。特别地,由于也可以以与本地日期时间相同的格式来写入期间,例如,3年6个月4天12小时30分5秒的P0003-06-04T12:30:05
,因此可能需要区分P
。P
还提供了一些快速方便的验证,以防您碰巧在预期持续时间的位置传递了一个完全不同的字符串。是的,PT10S
看起来很奇怪,但是一旦你习惯了它,你马上就会认识到它是一个持续时间,它可以是日期部分和时间部分之间的时间的practical.T
,这是基于两个原因选择的:T
的日期-时间字符串保持一致,例如,7月4日的2018-07-04T15:00
,2018年15:00点。M
:P3M
明确地表示3个月,而PT3M
表示3个月
发布于 2020-08-06 16:38:35
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);
}
}
https://stackoverflow.com/questions/51168022
复制相似问题