Link to home
Start Free TrialLog in
Avatar of akamauu
akamauu

asked on

How to convert java.util.Date to java.sql.Date or java.sql.Timestamp

I have a java.util.Date object stored in one of my objects and I want to insert it into a MS Access database.

patient.getPtDOB() returns a java.util.Date object.  I initially thought that I need to convert it to a java.sql.Date object in order to insert it into my database, but I understand that java.sql.Date does not contain time data.  So, should I convert it to java.sql.Date or java.sql.Timestamp?  Also, how do I do the conversion?

Thank you for your help!!!


***************************
Here is my code:

PreparedStatement ps = null;
String ptFName = patient.getPtFName();
String ptMName = patient.getPtMName();
String ptLName = patient.getPtLName();
Date ptDOB = patient.getPtDOB();

String insert = "INSERT INTO PATIENT (F_NAME, M_NAME, L_NAME, DOB) " + "VALUES (?, ?, ?, ?)";
ps = conn.prepareStatement(insert);
ps.setString(1, ptFName);
ps.setString(2, ptMName);
ps.setString(3, ptLName);
ps.setDate(4, ptDOB);
ps.executeUpdate();
ps.close();
Avatar of Manikandan Thiagarajan
Manikandan Thiagarajan
Flag of India image

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>but I understand that java.sql.Date does not contain time data.

Do you need to know what time the patient was born? ;-)
Avatar of akamauu
akamauu

ASKER

Thank you objects, that worked great.  Also, CEHJ, you are right when it comes to the patient's DOB I don't need to know the time, but I am doing the same thing with other java.util.Date objects that I do need to know the time.
>>Also, CEHJ, you are right when it comes to the patient's DOB I don't need to know the time

Yes, so be careful, since, if you're doing things like a query assembling people of the same age, be careful, as you could get unexpected results
Avatar of akamauu

ASKER

Thanks ... I will keep that in mind.
> Thank you objects, that worked great.

no worries :)