Take the 2-minute tour ×

Here is my problem: I have a user input a date like: 2012-12-24 (string) I concatenate a time to that string, and convert to java.util.Date My code looks like:

String tempstartdate = startdte;  //startdte is the string value from a txtfield
       tempstartdate += " 00:01:00";
       String tempenddate = startdte;
       tempenddate += " 23:59:59";

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");               
       java.util.Date validstartdte = null;
        java.util.Date validenddte = null;

validstartdte = df.parse(tempstartdate);  //validstartdte is a util.Date (and works)
validenddte = df.parse(tempenddate);

My problem is, when I take that util.Date and want to make it an sql.Date:

java.sql.Date sqlstartDate = new java.sql.Date(validstartdte.getTime());
java.sql.Date sqlendDate = new java.sql.Date(validenddte.getTime());

It will not give me the timestamp I assigned, it will only return the date in the form yyyy-MM-dd (such as 2012-12-23).

WHY!? I'm so frustrated.
Note: I noticed that when I used breakpoints, I was able to expand sqlendDate and see there is a value in there called cdate that returns: 2012-12-12T23:59:59.000-0500 The database I'm using is PostgreSQL.

Please help! Much appreciated.

share|improve this question
how are you getting that final format with the missing time of day? toString()? –  Chris Gerken Dec 21 '12 at 1:03

3 Answers 3

up vote 10 down vote accepted

java.sql.Date doesn't have the time.

Use java.sql.Timestamp instead.

share|improve this answer
Thank you so very much @Felipe Fonseca. This was my problem (the sql.Date vs sql.Timestamp), I overlooked this. It works now –  akwarywo Dec 21 '12 at 1:12
I know the original java class name is misleading but all they've done here is add to the confusion! –  Jonny Leeds Jun 4 at 15:55
Nono, the situation is worse. A java.sql.Date d does store the time internally (just doesn't display it), and can be turned back into a java.util.Date with new java.util.Date(d.getTime()). The constructor of java.sql.Date does not cut off the time information, but the toString method doesn't display it. But because the time information is hidden, two sql.Dates with the same string representation can be different! (!.equals()). Also, you have to be careful with mixing java.util.Date & java.sql.Timestamp, read the dos. What were they thinking when they designed these time classes... –  masterxilo Sep 4 at 11:32

I might be very late to answer this question but I think it might be helpful.

As stated by 'Felipe Fonseca', I converted the util date to sql date as follows:

public static java.sql.Timestamp convertToSqlDateTime(Date utilDate){
    return new java.sql.Timestamp(utilDate.getTime());

Normally, java.sql.Date only returns Date value and time will be discarded. So, in order to get time also, java.sql.TimeStamp must be used.

TimeStamp Constructs a Timestamp object using a milliseconds time value. The integral seconds are stored in the underlying date value; the fractional seconds are stored in the nanos field of the Timestamp object.

For this purpose, utilDate.getTime() is used to return the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date Object.

If we want only java.sql.Date, we can do:

public static java.sql.Date convertToSqlDate(Date utilDate){
    return new java.sql.Date(utilDate.getTime());
share|improve this answer

I have completely given up on using Java's standard Date classes, for exactly the reasons you list.

I've been using Joda Time for a while now, and have found it a lot simpler.

share|improve this answer

Your Answer


