Java – How to convert java.util.Date to java.sql.Date

datedatetimejavasql

I am trying to use a java.util.Date as input and then creating a query with it – so I need a java.sql.Date.

I was surprised to find that it couldn't do the conversion implicitly or explicitly – but I don't even know how I would do this, as the Java API is still fairly new to me.

Best Solution

Nevermind....

public class MainClass {

  public static void main(String[] args) {
    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);

  }

}

explains it. The link is http://www.java2s.com/Tutorial/Java/0040__Data-Type/ConvertfromajavautilDateObjecttoajavasqlDateObject.htm