Java – Convert java.util.Date() to java.sql.Date()

 

package com.pwn.dateUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class DateToSQLDateConversion {

	public static void main(String[] args) {
		java.util.Date utilDate = new java.util.Date();
		System.out.println("java.util.Date is : " + utilDate);
		java.sql.Date sqlDate = convert(utilDate);
		System.out.println("java.sql.Date is : " + sqlDate);
		DateFormat df = new SimpleDateFormat("dd/MM/YYYY - hh:mm:ss");
		System.out.println(
				"dateFormated date is : " + df.format(utilDate));

	}

	private static java.sql.Date convert(java.util.Date utilDate) {
		java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
		return sqlDate;
	}

}

java.util.Date is : Mon Jan 29 16:53:36 PST 2018
java.sql.Date is : 2018-01-29
dateFormated date is : 29/01/2018 - 04:53:36

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s