views:

1030

answers:

1

I have Informix database with timestamp field defined as YEAR TO SECOND. When I show this field using JDBC rs.getString(column) it uses format with miliseconds so this field looks like:

2008-12-18 13:58:14.0

I would like it to use only YEAR TO SECOND fields. I set environment variable:

GL_DATETIME=%Y-%m-%D %H:%M:%S

but even then I got miliseconds. Programs using ODBC do not show milisecond. How can I receive TIMESTAMP string "YEAR TO SECOND" only? In my program I can check metadata if field is TIMESTAMP and then cut ".0", but I think there should be simplier way.

Server version: IBM Informix Dynamic Server Version 11.50.TC2DE

Client version: IBM Informix JDBC Driver for IBM Informix Dynamic Server 3.50.JC3DE

EDIT It looks that all other JDBC drivers I tested (Oracle and PostgreSQL) shows Timestamp columns with miliseconds if I use getString(). So I used solution proposed by Todd. I check metatdata and if column is Timestamp then I use getTimestamp() and format it.

+3  A: 

If you are using JDBC, you can use the rs.getDate(column) or rs.getTimestamp(column) methods, which return Date and Timestamp objects respectively. Then you have an object representing time, rather than a String expressing it directly. With Date or Timestamp, you can use a date formatter to format it to whatever String representation of that time you choose.

Update (after reading comments below): If you use getDate(), it will still work for Timestamp columns. It will just reduce the precision down to the second. That way you don't have to check the metadata, you just have to know that the column is some kind of timestamp or date.

Todd
OK. But my program do reports and users can choose any table and field, so this solution needs checking ResultSet metadata if returned column is of Date/Timestamp type. I thought about more general solution.
Michał Niklas