java.time.Period.parse() 方法示例



說明

java.time.Period.parse(CharSequence text) 方法從文字字串中獲取一個週期,例如 PnYnMnD 和 PnW。

宣告

以下是對 java.time.Period.parse(CharSequence text) 方法進行的宣告。

public static Period parse(CharSequence text)

public static Period parse(CharSequence text)

引數

text - 要解析的文字,不能為空。

返回值

已解析的週期,不能為空。

異常

DateTimeParseException - 如果無法將文字解析為週期。

示例

package com.tutorialspoint;

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
列印頁面
廣告
© . All rights reserved.