localdatetime转换成String
时间: 2023-08-27 15:20:58 浏览: 117
要LocalDateTime类型转换为String类型,可以使用DateTimeFormatter类的ofPattern方法指定日期格式,然后使用format方法将LocalDateTime对象格式化为字符串。以下是示例代码:
```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 dateTime = LocalDateTime.now();
String dateString = dateTime.format(formatter);
System.out.println(dateString);
}
}
```
在这个示例中,我们创建了一个DateTimeFormatter对象,并使用ofPattern方法指定日期格式(例如"yyyy-MM-dd HH:mm:ss")。然后,我们获取当前的LocalDateTime对象,并使用format方法将其转换为字符串。最后,我们将字符串输出到控制台。<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^chatsearchT3_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^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [Java爬坑 -- LocalDateTime与String日期互相转换](https://blog.csdn.net/aizhuo7229/article/details/102089894)[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^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文