LocalDateTime 的 withDayOfYear() 方法
Java 中 LocalDateTime 类的 withDayOfYear() 方法用于以所需方式更改日期。该方法需要一个参数,即要设置在 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。然后使用 withDayOfYear() 方法显示日期已更改为 5 的 LocalDateTime。展示该内容的代码片段如下 −
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);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP