Java program to convert java.util.Date to java.sql.Date and vice versa

Here learn how to convertĀ java.util.Date to java.sql.Date. The following Java program is to convert current date from java.util.Date to java.sql.Date and vice versa.

java.util.Date to java.sql.Date

Syntax: java.util.Date ud=new java.util.Date();

Using the above code util Date is obtained.

Synatx: java.sql.Date sd=new java.sql.Date(ud.getTime());

Using getTime() method utilDate is converted to sqlDate.

java.sql.Date to java.util.Date

Syntax: java.sql.Date sd1=new java.sql.Date(System.currentTimeMillis());

The above code shows the sqlDate which is obtained using System.currentTimeMillis() method.

Using the same getTime() method it is converted to util Date.

package com.candidjava.time;

public class DateUtilToSql 
public static void main(String[] args) 
	//java.util.Date to java.sql.Date
	java.util.Date ud=new java.util.Date();
	java.sql.Date sd=new java.sql.Date(ud.getTime());
	//java.sql.Date to java.util.Date
	java.sql.Date sd1=new java.sql.Date(System.currentTimeMillis());
	java.util.Date ud1=new java.util.Date(sd1.getTime());


Mon Jan 07 13:32:01 IST 2019

Leave a Reply

Your email address will not be published. Required fields are marked *