LocalDateTime withDayOfYear()方法
使用withDayOfYear()Java中LocalDateTime类中的方法,可以根据需要更改一年中的某天的LocalDateTime的不可变副本。此方法需要一个参数,即要在LocalDateTime中设置的年月日,并且它返回LocalDateTime,并根据需要更改了年月日。
演示此的程序如下所示-
示例
import java.time.*;
public class Main {
public static void main(String[] args) {
LocalDateTime ldt1 = LocalDateTime.parse("2019-02-18T23:15:30");
System.out.println("The LocalDateTime is: " + ldt1);
LocalDateTime ldt2 = ldt1.withDayOfYear(5);
System.out.println("The LocalDateTime with day of year altered is: " + ldt2);
}
}输出结果
The LocalDateTime is: 2019-02-18T23:15:30 The LocalDateTime with day of year altered is: 2019-01-05T23:15:30
现在让我们了解上面的程序。
首先显示LocalDateTime。然后使用方法显示将一年中的日期更改为5的LocalDateTimewithDayOfYear()。演示这的代码片段如下-
LocalDateTime ldt1 = LocalDateTime.parse("2019-02-18T23:15:30");
System.out.println("The LocalDateTime is: " + ldt1);
LocalDateTime ldt2 = ldt1.withDayOfYear(5);
System.out.println("The LocalDateTime with day of year altered is: " + ldt2);热门推荐
10 同事换单位简短祝福语
11 谢谢朋友的祝福语简短
12 立秋节气简短祝福语
13 新房祝福语简短10字
14 长辈新年祝福语大全简短
15 新年给大家简短祝福语
16 生日祝福语简短独特姑姑
17 芭比的婚礼祝福语简短
18 邀请新人祝福语简短英文