Java LocalDateTime转字符串: yyyy-MM-dd HH:mm:ss格式
你可以使用DateTimeFormatter类来将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");
String formattedDateTime = now.format(formatter);
System.out.println(formattedDateTime);
}
}
运行这段代码将输出当前日期和时间的字符串表示,格式为yyyy-MM-dd HH:mm:ss。
原文地址: https://www.cveoy.top/t/topic/pSt0 著作权归作者所有。请勿转载和采集!