去掉LocalDateTime的T
时间: 2023-11-15 17:56:49 浏览: 167
可以使用DateTimeFormatter将LocalDateTime格式化为字符串,然后将T替换为空格即可去掉T。示例代码如下:
```
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
String formattedDateTime = now.format(formatter).replace("T", " ");
System.out.println(formattedDateTime);
```
相关问题
LocalDateTime 去掉T
LocalDateTime是Java 8中的一个时间类,它表示一个不带时区信息的日期和时间。在LocalDateTime中,T是指日期和时间的分隔符。如果您想去掉T,可以使用DateTimeFormatter类将LocalDateTime对象转换为您需要的日期和时间格式。以下是一个示例代码:
```
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.format(formatter);
System.out.println(formattedDateTime);
```
这里我们使用了DateTimeFormatter的ofPattern方法创建了一个指定格式的日期时间格式化对象,并使用LocalDateTime的format方法将其转换为我们需要的字符串格式。
LocalDateTime去掉T
你可以使用`LocalDateTime`的`format`方法和`DateTimeFormatter`来去掉字符串中的T。具体代码如下所示:
```java
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`格式化成了指定的字符串格式,其中"yyyy-MM-dd HH:mm:ss"表示年份、月份、日期、小时、分钟和秒数。运行该代码会得到不含T的时间字符串。
相关推荐
![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)