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

Posted on 2006-04-11
Last Modified: 2011-08-18
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);
Question by:akamauu
    LVL 8

    Expert Comment

    LVL 92

    Accepted Solution

    > ps.setDate(4, ptDOB);

    change that to:

    ps.setTimestamp(4, new java.sql.Timestamp(ptDOB.getTime()));
    LVL 86

    Expert Comment

    >>but I understand that java.sql.Date does not contain time data.

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

    Author Comment

    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.
    LVL 86

    Expert Comment

    >>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

    Author Comment

    Thanks ... I will keep that in mind.
    LVL 92

    Expert Comment

    > Thank you objects, that worked great.

    no worries :)

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    tenRun challenge 28 73
    EvenOdd challenge 10 70
    network + 7 60
    mapShare challenge 13 43
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    This video teaches viewers about errors in exception handling.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now