Java中的LocalDate withDayOfYear()方法

使用withDayOfYear()Java中LocalDate类中的方法,可以根据需要更改年份的不可变副本。此方法需要一个参数,即要在LocalDate中设置的一年中的日期,并且它返回LocalDate并根据需要更改年份。

演示此的程序如下所示-

示例

import java.time.*;
public class Main {
   public static void main(String[] args) {
      LocalDate ld1 = LocalDate.parse("2019-02-15");
      System.out.println("The LocalDate is: " + ld1);
      LocalDate ld2 = ld1.withDayOfYear(05);
      System.out.println("The LocalDate with day of year altered is: " + ld2);
   }
}

输出结果

The LocalDate is: 2019-02-15
The LocalDate with day of year altered is: 2019-01-05

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

首先显示LocalDate。然后使用方法显示将一年中的日期更改为05的LocalDate withDayOfYear()。演示这的代码片段如下-

LocalDate ld1 = LocalDate.parse("2019-02-15");
System.out.println("The LocalDate is: " + ld1);
LocalDate ld2 = ld1.withDayOfYear(05);
System.out.println("The LocalDate with day of year altered is: " + ld2);