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

package org.kodejava.example.sql;

import java.util.Date;

public class UtilDateToSqlDate {
    public static void main(String[] args) {
        //
        // Create a new instance of java.util.Date
        //
        java.util.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);
    }
}

Wayan Saryada

I am a programmer, a runner, an open water diver and currently living in the island of Bali, Indonesia. If you need help on Java programming you can hire me on Fiverr.

Leave a Comment.