Skip to content
Advertisement

JDBC ResultSet: I need a getDateTime, but there is only getDate and getTimeStamp

I would like to get the DATETIME column from an Oracle DB Table with JDBC. Here is my code:

int columnType = rsmd.getColumnType(i);
if(columnType == Types.DATE)
{
    Date aDate = rs.getDate(i);
    valueToInsert = aDate.toString();
}
else if(columnType == Types.TIMESTAMP)
{
    Timestamp aTimeStamp = rs.getTimestamp(i);
    valueToInsert = aTimeStamp.toString();
}
else
{
    valueToInsert = rs.getString(i);
}

I have to identify the column type first. The field I am interested in is recognized as a Types.DATE, but it is really a DATETIME in the DB since it has this format: “07.05.2009 13:49:32”

getDate truncates the time: “07.05.2009” and getString appends “.0” to it: “07.05.2009 13:49:32.0”

Of course I could just remove the final .0 and work with getString all the time, but it is a dirty workaround.

Any ideas? I was looking for a getDateTime method.

Advertisement

Answer

This answer is outdated. Continue to Basil Bourque’s answer.

java.util.Date date;
Timestamp timestamp = resultSet.getTimestamp(i);
if (timestamp != null)
    date = new java.util.Date(timestamp.getTime()));

Then format it the way you like.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement