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

2019-10-16 22:47:27

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

宣告

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

public LocalDateTime withYear(int year)

引數

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

返回值

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

異常

  • DateTimeException - 如果年份值無效。

範例

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

package com.yiibai;

import java.time.LocalDateTime;

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

      LocalDateTime date = LocalDateTime.parse("2017-01-03T10:15:30");
      LocalDateTime result = date.withYear(2016);
      System.out.println(result);  
   }
}

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

2016-01-03T10:15:30