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



描述

java.time.YearMonth.withYear(int 年份) 方法返回由此年月的年份更改後的副本。

宣告

以下是 java.time.YearMonth.withYear(int 年份) 方法的宣告。

public YearMonth withYear(int year)

引數

年份 − 在返回的年月中要設定的年份,從 MIN_YEAR 到 MAX_YEAR。

返回值

基於此,進行了調整的 YearMonth,非空。

異常

DateTimeException − 如果年份值無效。

示例

以下示例演示瞭如何使用 java.time.YearMonth.withYear(int 年份) 方法。

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.withYear(2008);
      System.out.println(result);  
   }
}

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

2008-12
廣告
© . All rights reserved.