當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.time.YearMonth.withYear()用法及代碼示例


描述

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

聲明

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

public YearMonth withYear(int year)

參數

year- 在返回的 year-month 中設置的年份,從 MIN_YEAR 到 MAX_YEAR。

返回值

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

異常

DateTimeException− 如果年份值無效。

示例

下麵的例子展示了 java.time.YearMonth.withYear(int year) 方法的用法。

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

相關用法


注:本文由純淨天空篩選整理自 java.time.YearMonth.withYear() Method Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。