String转LocalDataTime
时间: 2024-01-18 16:18:47 浏览: 51
在Java中,可以使用DateTimeFormatter类来将String类型的时间转换为LocalDateTime类型。下面是一个示例代码:
```java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class StringToLocalDateTime {
public static void main(String[] args) {
String strDateTime = "2020-11-09 18:32:48";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(strDateTime, formatter);
System.out.println("String类型的时间转成LocalDateTime:" + dateTime);
}
}
```
输出结果为:
```
String类型的时间转成LocalDateTime:2020-11-09T18:32:48
```
相关问题
localdatatime转string
将LocalDateTime转换为字符串的方法是使用DateTimeFormatter类的format方法。例如:
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.format(formatter);
这将把当前的LocalDateTime对象格式化为字符串,使用指定的日期时间格式。在这个例子中,格式是“年-月-日 时:分:秒”。
Data转LocalDataTime
将LocalDateTime转换为字符串的方法是使用DateTimeFormatter类的format方法。例如:
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.format(formatter);
这将把当前的LocalDateTime对象格式化为字符串,使用指定的日期时间格式。在这个例子中,格式是“年-月-日 时:分:秒”。
相关推荐
![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)