java.time.YearMonth.withYear(int year)方法

java.time.YearMonth.withYear(int year)方法返回此YearMonth的副本,其中年份已更改。

聲明

以下是java.time.YearMonth.withYear(int year)方法的聲明。

public YearMonth withYear(int year)

參數

  • year - 在返回的年月中設置的年份,從MIN_YEARMAX_YEAR

返回值

基於此YearMonth進行調整的YearMonth,而不是null

例外

  • DateTimeException - 如果年份值無效。

示例

以下示例顯示了java.time.YearMonth.withYear(int year)方法的用法。

package com.zaixian;

import java.time.YearMonth;

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

      YearMonth date = YearMonth.parse("2017-12");
      YearMonth result = date.withYear(2008);
      System.out.println(result);
   }
}

編譯並運行上面的程式,這將產生以下結果 -

2008-12

上一篇: java.time.YearMonth類 下一篇: java.time.ZonedDateTime類