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



說明

java.time.YearMonth.with(TemporalField field, long newValue) 方法使用指定的值返回此日期的副本,其中指定欄位設定為新值。

宣告

以下是 java.time.YearMonth.with(TemporalField field, long newValue) 方法的宣告。

public YearMonth with(TemporalField field, long newValue)

引數

  • field − 要在結果中設定的欄位,不能為空。

  • newValue − 結果中欄位的新值。

返回值

基於此項的 YearMonth,進行了調整,不能為空。

異常

  • DateTimeException − 如果無法進行調整。

  • UnsupportedTemporalTypeException − 如果不支援此欄位。

  • ArithmeticException − 如果發生數值溢位。

示例

以下示例展示了 java.time.YearMonth.with(TemporalField field, long newValue) 方法的使用。

package com.tutorialspoint;

import java.time.YearMonth;
import java.time.temporal.ChronoField;

public class YearMonthDemo {
   public static void main(String[] args) {

      YearMonth date = YearMonth.parse("2017-12");
      YearMonth result = date.with(ChronoField.YEAR,2013);
      System.out.println(result);  
   }
}

讓我們編譯並執行上述程式,這將生成以下結果 -

2013-12
廣告
© . All rights reserved.