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.