diff --git a/src/chapter5/Example23.java b/src/chapter5/Example23.java index 4b18e7a..7c51a44 100644 --- a/src/chapter5/Example23.java +++ b/src/chapter5/Example23.java @@ -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"))); + } }