LocalDateTime怎么转化为string
时间: 2023-12-07 14:40:06 浏览: 33
可以使用DateTimeFormatter将LocalDateTime转化为String类型的时间。具体实现方法如下:
```java
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime time = LocalDateTime.now();
String localTime = df.format(time);
System.out.println("LocalDateTime转成String类型的时间:"+localTime);
```
其中,DateTimeFormatter.ofPattern()方法中的参数是时间格式,可以根据需要进行修改。
相关问题
LocalDateTime转换为string格式
可以使用DateTimeFormatter将LocalDateTime转换为string格式,例如:
```
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.format(formatter);
System.out.println(formattedDateTime);
```
输出结果类似于:2021-08-31 14:30:00
java如何将LocalDateTime转换为String
可以使用DateTimeFormatter类将LocalDateTime转换为String。
以下是一个示例代码:
```java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeToStringExample {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatDateTime = now.format(formatter);
System.out.println("Formatted LocalDateTime: " + formatDateTime);
}
}
```
输出:
```
Formatted LocalDateTime: 2020-12-08 16:30:45
```
在上面的示例中,我们首先创建了一个LocalDateTime对象,然后使用DateTimeFormatter.ofPattern方法创建一个格式化器。最后,我们使用format方法将LocalDateTime对象格式化为String。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)