LocalDateTime

This commit is contained in:
seahi 2024-11-13 21:51:35 +08:00
parent 9c663672aa
commit c26c27561c

View File

@ -1,4 +1,19 @@
package chapter5;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Example23 {
public static void main(String[] args) {
// 1. 获取当前时间
LocalDateTime now = LocalDateTime.now();
System.out.println("原格式:" + now);
System.out.println("只有日期:" + now.toLocalDate());
System.out.println("只有时间:" + now.toLocalTime());
// 2. getter 方法
System.out.println(now.getYear() +"" + now.getMonthValue() + "" + now.getDayOfMonth() + ""
+ now.getHour() + "" + now.getMinute() + "" + now.getSecond() + "");
// 3. 指定格式
System.out.println("指定格式:" + now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
}