When working with dates in Java, you may encounter the need to convert between java.util.Date and java.sql.Date. While you might expect implicit or explicit type conversion, the Java API requires an explicit conversion process. This guide will delve into the solution and provide a clear explanation of the code required.
The code snippet below demonstrates how you can convert a java.util.Date object to a java.sql.Date object:
java.util.Date utilDate = new java.util.Date(); java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); System.out.println("utilDate:" + utilDate); System.out.println("sqlDate:" + sqlDate);
In this code, the following steps take place:
The above is the detailed content of How to Convert Between java.util.Date and java.sql.Date in Java?. For more information, please follow other related articles on the PHP Chinese website!