Java LocalDateTime 获取当前时间并格式化显示 年月日 时分秒 星期几
可以使用如下代码实现:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss E');
String formattedDateTime = now.format(formatter);
System.out.println(formattedDateTime);
}
}
输出结果类似于:
2021-08-31 15:30:45 Tue
其中,LocalDateTime.now() 获取当前时间,DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss E') 定义了输出格式,now.format(formatter) 将当前时间格式化成指定格式的字符串,最后输出即可。
原文地址: http://www.cveoy.top/t/topic/oUBc 著作权归作者所有。请勿转载和采集!