LocalDateTime 格式化
时间: 2023-11-09 10:56:51 浏览: 43
LocalDateTime可以通过DateTimeFormatter来进行格式化。在提供的引用中,使用了DateTimeFormatter.ofPattern方法来定义格式模式,然后使用format方法将LocalDateTime对象格式化为指定格式的字符串。例如,可以使用"yyyy-MM-dd HH:mm:ss"作为格式模式来格式化LocalDateTime对象。
另外,还可以使用其他方法来对LocalDateTime进行格式化。例如,可以使用LocalDateTime的format方法直接传入格式模式来格式化对象。
示例代码如下:
```java
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("Formatted DateTime: " + formattedDateTime);
}
}
```