LocalDate 转为date
时间: 2023-10-31 17:18:17 浏览: 110
LocalDate和Date的相互转化
要将LocalDate转换为Date,可以使用以下方法之一:
1. 使用LocalDate.atStartOfDay方法:
```java
LocalDate localDate = LocalDate.now(); // 或者从其他方法获取LocalDate对象
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
```
2. 使用LocalDateTime.of方法:
```java
LocalDate localDate = LocalDate.now(); // 或者从其他方法获取LocalDate对象
LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIDNIGHT);
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
```
3. 使用Timestamp.valueOf方法:
```java
LocalDate localDate = LocalDate.now(); // 或者从其他方法获取LocalDate对象
Timestamp timestamp = Timestamp.valueOf(localDate.atStartOfDay());
Date date = new Date(timestamp.getTime());
```
请注意,这些方法都需要指定时区。上述示例中使用了系统默认的时区(ZoneId.systemDefault())。你也可以根据需要使用其他时区。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [LocalDate和Date相互转换](https://blog.csdn.net/qq_33732195/article/details/121063440)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [【Java 8 新特性】Java LocalDate 转 Date](https://blog.csdn.net/qq_31635851/article/details/120132051)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文