日期时间

创建日期:2024-06-21
更新日期:2024-12-10

获取当前时间(旧版)

Date date = new Date();

String str = String.format("%tY-%tm-%td %tH:%tM:%tS", // 或:%tF %tT
   date, date, date, date, date, date); // Y-m-d H:M:s => 2021-07-19 08:33:03

获取当前时间(新版)

LocalDateTime now = LocalDateTime.now();
System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

LocalDateTime转DateTime

LocalDateTime now = LocalDateTime.now();
Date date = Date.from(Instant.from(now.atZone(ZoneId.systemDefault())));
System.out.printf("%tF %tT\n", date, date);