我正在尝试使用
Duration
类而不是 long
。
它具有优越的文字语法。我喜欢它的灵活性,尽管它看起来很奇怪。
“PT10S”的意思是10秒,接受“10秒”有什么问题?! 好吧,没关系。
我只是好奇为什么选择 PT 前缀(例如不是“DU”)以及为什么这里任何前缀都比没有更好?
可以在 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
表示“期间”,并且因为没有日期组件,所以它只有一个“时间”。
您可以将其解释为“一段时间”
选择这个的“原因”,你必须询问编写该标准的 ISO 成员,但我的猜测是它更容易解析。 (简短且明确)
时间部分的详细信息是:
H is the hour designator that follows the value for the number of hours.
M is the minute designator that follows the value for the number of minutes.
S is the second designator that follows the value for the number of seconds.
PT20S
的值解析为:
因此,持续时间为 20 秒。
更多示例可以在javadoc中找到:https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#parse-java.lang.CharSequence-
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 分钟。实际上,如果继续从 1.8 开始使用 Java 开发 Duration API,他们已经采用标准 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);
}
}