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



說明

java.time.LocalDate.withYear(int year) 方法返回此 LocalDate 的時間,其中年份已更改。

宣告

以下是 java.time.LocalDate.withYear(int year) 方法的宣告。

public LocalDate withYear(int year)

引數

年份 − 在結果中設定的年份,介於 MIN_YEAR 到 MAX_YEAR。

返回值

基於此日期並具有請求年份的 LocalDate,不為 null。

異常

DateTimeException − 如果年份值無效。

示例

以下示例展示瞭如何使用 java.time.LocalDate.withYear(int year) 方法。

package com.tutorialspoint;

import java.time.LocalDate;

public class LocalDateDemo {
   public static void main(String[] args) {
      
      LocalDate date = LocalDate.parse("2017-01-03");
      LocalDate result = date.withYear(2016);
      System.out.println(result);  
   }
}

讓我們編譯並執行以上程式,它會生成以下結果 −

2016-01-03
廣告
© . All rights reserved.