LocalDateTime 转String
时间: 2023-10-14 15:02:40 浏览: 100
要将LocalDateTime类型转换为String类型,可以使用DateTimeFormatter类进行格式化。以下是一种实现方法:
```java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// 指定日期格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 获取当前日期时间
LocalDateTime now = LocalDateTime.now();
// LocalDateTime转String
String dateStr = now.format(formatter);
System.out.println("LocalDateTime转换为String后的结果:" + dateStr);
}
}
```
在上面的代码中,我们首先创建了一个DateTimeFormatter对象,用于指定日期格式。然后,使用LocalDateTime.now()获取当前的日期时间。接下来,使用format方法将LocalDateTime对象转换为String类型,将其存储在dateStr变量中。最后,我们将结果打印出来。
<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [LocalDateTime与String日期互相转换](https://blog.csdn.net/m0_49692893/article/details/127220722)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [LocalDateTime与String之间的转换](https://blog.csdn.net/m0_51347332/article/details/119802719)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [LocalDateTime、LocalDate、Date、String相互转化大全及其注意事项](https://blog.csdn.net/qq_52423918/article/details/130127423)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文