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



說明

java.time.LocalDateTime.withDayOfYear(int dayOfYear) 方法返回一個複製的 LocalDateTime,其年份天數已更改。

宣告

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

public LocalDateTime withDayOfYear(int dayOfYear)

引數

dayOfYear - 在結果中設定的年份天數,從 1 到 365-366。

返回值

基於此日期的 LocalDateTime,帶有請求的日期,不為 null。

異常

DateTimeException - 如果年份天數的值無效,或如果年份天數對於該年份無效。

示例

以下示例展示了 java.time.LocalDateTime.withDayOfYear(int dayOfYear) 方法的用法。

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.withDayOfYear(43);
      System.out.println(result);  
   }
}

讓我們編譯並執行以上程式,這將產生以下結果 -

2017-02-12T10:15:30
廣告
© . All rights reserved.