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

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

聲明

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

public LocalDate withYear(int year)

參數

  • year - 結果中設置的年份,從MIN_YEARMAX_YEAR

返回值

基於此日期的LocalDate與請求的年份,不為null

異常

  • DateTimeException - 如果年份值無效。

示例

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

package com.zaixian;

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

上一篇: java.time.LocalDate類 下一篇: java.time.LocalDateTime類