在java中如何获取当前时间
使用Java的DateTimeFormatter格式化当前时间
Java 8及以后版本推荐使用java.time包中的LocalDateTime类获取当前时间。你可以通过LocalDateTime.now()获取当前时间,然后使用DateTimeFormatter进行格式化,例如:
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.format(formatter);
System.out.println(formattedDateTime);
这样就可以得到年-月-日 时:分:秒格式的当前时间。