Java中的LocalDate getDayOfYear()方法

可以使用getDayOfYear()Java中LocalDate类中的方法来获取特定LocalDate的日期。此方法不需要任何参数,它会返回一年中的日期,该日期可以在1到365之间,对于leap年也可以是366。

演示此程序如下

示例

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

输出结果

The LocalDate is: 2019-02-14
The day of the year is: 45

现在让我们了解上面的程序。

首先显示LocalDate。然后使用该getDayOfYear()方法显示LocalDate的日期。演示此代码段如下所示:

LocalDate ld = LocalDate.parse("2019-02-14");
System.out.println("The LocalDate is: " + ld);
System.out.println("The day of the year is: " + ld.getDayOfYear());