2

How come that java.sql.PreparedStatement#setDate on dateTime mysql column always results in Date format like 2012-10-16 00:00:00 without Time information ? I'm storing Date in a loop like this :

protected Date getIncrementedDateTime() {
    additionalTime += 1000;
    return new Date(System.currentTimeMillis() + additionalTime);
}


preparedStatement.setDate(j, new java.sql.Date(getIncrementedDateTime().getTime());

I debugged the values, but mysql database always have 2012-10-16 00:00:00 values.

0

3 Answers 3

3

As per the documentation for java.sql.Date:

A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. [...]

To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

Basically, for a datetime column you should probably be using a different type, e.g. Timestamp. (That's not ideal in other ways, but it's likely to be a better fit...)

Sign up to request clarification or add additional context in comments.

4 Comments

Timestamp doesn't go with 'dateTime' column type right ? Incorrect datetime value: '16:16:51' for column ... So that I need to use 'timeStamp' column type and Timestamp data type ?
@Sloin: I suspect that Timestamp will actually work fine with a datetime column. It's basically an omission in JDBC here, but Timestamp is probably as close as you can get.
You're right, java.sql.Timestamp and ps.setTimestamp(j, (Timestamp) value); works fine with 'dateTime' column type
@Sloin Timestamp should work just fine. Try doing this: preparedStatement.setDate(j, new java.sql.Timestamp(getIncrementedDateTime().getTime());
0

A java.sql.Date is just a date -- no time. Do not confuse it with java.util.Date, which includes time information. You might want to look at java.sql.Timestamp.

Comments

0

It is base on the data type of your table's column. Try to use TimeStamp or Time according to Database Server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.