LocalDateTime转 java.sql.Date
时间: 2023-10-15 20:22:36 浏览: 184
LocalDateTime转换为java.sql.Date的方法是将LocalDateTime先转换为LocalDate,然后再将LocalDate转换为java.sql.Date。可以使用toLocalDate()方法将LocalDateTime转换为LocalDate,然后使用valueOf(LocalDate)方法将LocalDate转换为java.sql.Date。以下是一个示例代码:
```java
import java.time.LocalDateTime;
import java.time.LocalDate;
import java.sql.Date;
public class Test {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.now();
System.out.println("LocalDateTime = " + dateTime);
LocalDate localDate = dateTime.toLocalDate();
System.out.println("LocalDate = " + localDate);
Date sqlDate = Date.valueOf(localDate);
System.out.println("java.sql.Date = " + sqlDate);
}
}
```
在这个示例中,先创建了一个LocalDateTime对象dateTime,然后使用toLocalDate()方法将其转换为LocalDate对象localDate,最后使用valueOf(LocalDate)方法将localDate转换为java.sql.Date对象sqlDate。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [java8 LocalDateTime/LocalDate/LocalTime、java.util.Date/java.sql.Date区别及日期转换方法](https://blog.csdn.net/wangjun5159/article/details/115002441)[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_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [Java8中 LocalDate和java.sql.Date的相互转换](https://blog.csdn.net/hspingcc/article/details/73332125)[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_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文