Java: ResultSet getString() differs between environments

datetimeformattingjavaoraclesql

I have a SQL query that is returning an oracle Date object.
e.g.:

SELECT sysdate FROM DUAL

There is code currently that does the following:

String s = rs.getString("sysdate");

The problem is, this returns different date format on different environments (database is the same).

One environment will return:
2011-01-31 12:59:59.0

The other will return something weirder:
2011-1-31 12.15.32.0 (the time is separated by decimals)

Maybe this has something do with Locale… one machine is "English (Canada)" as reported by java, the other is "English (United States)".

What I'm interested in is, when the resultset transforms the date object into a string, where is that format coming from?

Best Answer

From Oracle's site:

At database connection time, the JDBC Class Library sets the server NLS_LANGUAGE and NLS_TERRITORY parameters to correspond to the locale of the Java VM that runs the JDBC driver

So yes, the difference in the response is because the machines have a different locale specified. The correct solution should be to use getDate() or getTimestamp() or have the database server return the date as a string in a specific format as mentioned earlier.