Java Program to convert from a java.util.Date Object to a java.sql.Date Object

Java 8Object Oriented ProgrammingProgramming

First, let us create a java.util.Date object.

// util object
java.util.Date utilObj = new java.util.Date();

Now, create a java.sql.Date object.

java.sql.Date sqlObj = new java.sql.Date();

Converting from a java.util.Date object to a java.sql.Date object.

java.sql.Date sqlObj = new java.sql.Date(utilObj.getTime());

The following is an example.

Example

 Live Demo

import java.util.Date;
public class Example {
   public static void main(String args[]) {
      // util object
      java.util.Date utilObj = new java.util.Date();
      // sql object
      java.sql.Date sqlObj = new java.sql.Date(utilObj.getTime());
      System.out.println("Util Date = " + utilObj);
      System.out.println("SQL Date = " + sqlObj);
   }
}

Output

Util Date = Mon Nov 19 05:48:00 UTC 2018
SQL Date = 2018-11-19
raja
Published on 17-Dec-2018 09:46:19
Advertisements