20 lines
805 B
Java
20 lines
805 B
Java
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")));
|
|
}
|
|
}
|