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



說明

java.time.YearMonth.parse(CharSequence text) 方法從文字字串(例如 2017-12)獲取 YearMonth 例項。

宣告

以下是 java.time.YearMonth.parse(CharSequence text) 方法的宣告。

public static YearMonth parse(CharSequence text)

引數

text − 要解析的文字,例如 "2017-12",不能為空。

返回值

年月,不能為空。

異常

DateTimeParseException - 如果無法解析文字。

示例

下面的示例展示了 java.time.YearMonth.parse(CharSequence text) 方法的用法。

package com.tutorialspoint;

import java.time.YearMonth;

public class YearMonthDemo {
   public static void main(String[] args) {
 
      YearMonth date = YearMonth.parse("2017-12");
      System.out.println(date);  
   }
}

讓我們編譯並執行上面的程式,它將產生以下結果 −

2017-12
廣告
© . All rights reserved.