java.time.Period.parse(CharSequence text)
方法从文本字符串(例如PnYnMnD
和PnW
)获取Period
。
声明
以下是java.time.Period.parse(CharSequence text)
方法的声明。
public static Period parse(CharSequence text)
参数
text
- 要解析的文本,而不是null
。
返回值
解析后的Period
值,不为null
。
异常
DateTimeParseException
- 如果无法将文本解析为Period
。
示例
以下示例显示了java.time.Period.parse(CharSequence text)
方法的用法。
package com.yiibai;
import java.time.Period;
public class PeriodDemo {
public static void main(String[] args) {
Period period = Period.parse("P1Y2M3D");
System.out.println("Years: " + period.getYears()
+ ", Months: " + period.getMonths()
+", Days: " + period.getDays());
}
}
编译并运行上面的程序,这将产生以下结果 -
Years: 1, Months: 2, Days: 3