LocalDateTime 转成 yyyy-MM-dd HHmmss 格式字符串
要将LocalDateTime转换为yyyy-MM-dd HH:mm:ss格式的字符串,可以使用DateTimeFormatter类的ofPattern方法来指定格式。具体代码如下:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = dateTime.format(formatter);
        System.out.println(formattedDateTime);
    }
}
在上述代码中,首先获取当前的LocalDateTime对象,然后使用DateTimeFormatter类的ofPattern方法创建一个指定格式的DateTimeFormatter对象。然后,使用LocalDateTime对象的format方法将其格式化为字符串,并将结果输出。输出的字符串即为yyyy-MM-dd HH:mm:ss格式
原文地址: https://www.cveoy.top/t/topic/iySH 著作权归作者所有。请勿转载和采集!