Home > Database > Mysql Tutorial > How to Convert a `java.util.Date` to a `java.sql.Date`?

How to Convert a `java.util.Date` to a `java.sql.Date`?

Barbara Streisand
Release: 2025-01-21 10:42:10
Original
609 people have browsed it

How to Convert a `java.util.Date` to a `java.sql.Date`?

Convert java.util.Date to java.sql.Date

Question:

How to convert java.util.Date to java.sql.Date for use in queries?

Answer:

To explicitly convert java.util.Date to java.sql.Date, use the following method:

<code class="language-java">java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());</code>
Copy after login

Example:

<code class="language-java">import java.util.Date;
import java.sql.Date;

public class MainClass {

  public static void main(String[] args) {
    Date utilDate = new Date();
    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
    System.out.println("utilDate:" + utilDate);
    System.out.println("sqlDate:" + sqlDate);
  }
}</code>
Copy after login

Output:

<code>utilDate:Wed Jan 01 00:00:00 SGT 1970
sqlDate:1970-01-01</code>
Copy after login

Note:

The java.sql.Date class represents date values ​​in the SQL DATE data type. It is similar to the java.util.Date class, but only stores dates, not times.

The above is the detailed content of How to Convert a `java.util.Date` to a `java.sql.Date`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template