LocalDateTime 的 getDayOfYear() 方法


在 Java 中,可以使用 LocalDateTime 類的 getDayOfYear() 方法來獲取特定 LocalDateTime 的年度日。該方法不需要引數,並且它返回年度日,其範圍為 1 到 365,對於閏年,還為 366。

如下所示,一個演示此過程的程式:

示例

 線上示例

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      LocalDateTime ldt = LocalDateTime.parse("2019-02-18T11:30:15");
      System.out.println("The LocalDateTime is: " + ldt);
      System.out.println("The day of the year is: " + ldt.getDayOfYear());
   }
}

輸出

The LocalDateTime is: 2019-02-18T11:30:15
The day of the year is: 49

現在,我們來理解以上程式。

首先,顯示了 LocalDateTime。然後,使用 getDayOfYear() 方法顯示了 LocalDateTime 的年度日。如下所示的程式碼段演示了這一點:

LocalDateTime ldt = LocalDateTime.parse("2019-02-18T11:30:15");
System.out.println("The LocalDateTime is: " + ldt);
System.out.println("The day of the year is: " + ldt.getDayOfYear());

更新時間:2019 年 7 月 30 日

54 次瀏覽

啟動您的 職業

完成課程以獲得認證

開始
廣告