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



說明

java.time.LocalDateTime.withYear(int year) 方法返回一個 LocalDateTime 副本,其中 year 已更改。

宣告

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

public LocalDateTime withYear(int year)

引數

year − 在結果中設定的年份,從 MIN_YEAR 到 MAX_YEAR。

返回值

基於此日期且具有所請求年份的 LocalDateTime,非空。

異常

DateTimeException − 如果年份值無效。

示例

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

package com.tutorialspoint;

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
廣告
© . All rights reserved.