How do I convert java.util.Date to java.sql.Date?

package org.kodejava.jdbc;

import java.util.Date;

public class UtilDateToSqlDate {
    public static void main(String[] args) {
        // Create a new instance of java.util.Date
        Date date = new Date();

        // To covert java.util.Date to java.sql.Date we need to
        // create an instance of java.sql.Date and pass the long
        // value of java.util.Date as the parameter.
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());

        System.out.println("Date    = " + date);
        System.out.println("SqlDate = " + sqlDate);

The result of the code snippet above:

Date    = Sat Oct 09 20:23:27 CST 2021
SqlDate = 2021-10-09

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.