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



說明

java.time.YearMonth.withMonth(int month) 方法返回一個 copy,其中該 YearMonth 的月份已更改。

宣告

以下是 java.time.YearMonth.withMonth(int month) 方法的宣告。

public YearMonth withMonth(int month)

引數

month − 在返回的年中設定月份,從 1(1 月)到 12(12 月)。

返回值

基於此 YearMonth 的 YearMonth,已進行調整,非 null。

異常

DateTimeException − 如果月份的值無效。

示例

以下示例演示了 java.time.YearMonth.withMonth(int month) 方法的用法。

package com.tutorialspoint;

import java.time.YearMonth;

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

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

2017-08
廣告
© . All rights reserved.