java的parse方法
持续时间类parse()方法 (Duration Class parse() method)
parse() method is available in java.time package.
parse()方法在java.time包中可用。
parse() method is used to return a Duration that parses the given char sequence and char sequence follow some ISO -8601 standard format like PnDTnHnMn.nS.
parse()方法用于返回一个Duration,该Duration解析给定的char序列,并且char序列遵循某些ISO -8601标准格式,例如PnDTnHnMn.nS。
parse() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
parse()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会出错。
parse() method may throw an exception at the time of parsing the given sequence.
parse()方法在解析给定序列时可能会引发异常。
DateTimeParseException: This exception may throw when the given parameter couldn't parse.
DateTimeParseException :如果无法解析给定参数,则可能引发此异常。
Syntax:
句法:
public static Duration parse(CharSequence cs);
Parameter(s):
参数:
CharSequence cs – represents the char sequence to parse to a Duration.
CharSequence cs –表示要解析为Duration的char序列。
Return value:
返回值:
The return type of this method is Duration, it returns the Duration that holds the parse value of the given sequence to a Duration.
此方法的返回类型为Duration ,它将将持有给定序列的解析值的Duration返回为Duration。
Example:
例:
// Java program to demonstrate the example
// of parse(CharSequence cs) method of Duration
import java.time.*;
public class ParseOfDuration {
public static void main(String args[]) {
// parses the given sequence in this Duration du1
// i.e. 1D:1H:20M:10S in a standard format PnDTnHnMn.S
Duration du1 = Duration.parse("P1DT1H20M10S");
// Display du1
System.out.println("Duration.parse(P1DT1H20M10S): " + du1);
// parsees the given sequence in this Duration du1
// i.e. 1D:1H: in a standard format PnDTnHnMn.S
Duration du2 = Duration.parse("P1DT1H");
// Display du2
System.out.println("Duration.parse(P1DT1H): " + du2);
}
}
Output
输出量
Duration.parse(P1DT1H20M10S): PT25H20M10S
Duration.parse(P1DT1H): PT25H
翻译自: https://www.includehelp.com/java/duration-parse-method-with-example.aspx
java的parse方法