I'm looking at some code that basically does the following:
ResultSet rs = ps.executeQuery();
String myDateStr = rs.getString("MY_DATE"); //field is of type Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'.0'");
Date myDate = sdf.parse(myDateStr);
On some environments the last line works, and on others it throws an Unparseable date exception. It looks like on some systems the default date format is 2013-01-25 00:00:00.0, and on others 2013-01-25 00:00:00. The JVM, OS and Oracle version are different between the environments (all use Oracle and run on a unix variant though).
Changing the code might be complex. I'm wondering if there is an environment variable or similar that can be set to make the date format returned from rs.getString() consistent?
The default ResultSet type is TYPE_FORWARD_ONLY . Note: Not all databases and JDBC drivers support all ResultSet types. The method DatabaseMetaData.
Execute the query using the executeQuery() method. Pass the select query to retrieve data (String) as a parameter to it. From the obtained result set object get the date value (along with other values) using the getDate() method of the ResultSet interface.
The SELECT statement is the standard way to select rows from a database and view them in a result set. The java. sql. ResultSet interface represents the result set of a database query. A ResultSet object maintains a cursor that points to the current row in the result set.
A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT. To conform with the definition of SQL DATE , the millisecond values wrapped by a java. sql.
try this:
ResultSet rs = ps.executeQuery();
Date myDate = rs.getDate("MY_DATE");
or this :
ResultSet rs = ps.executeQuery();
String myDateStr = rs.getString("MY_DATE");
Date myDate = valueOf(myDateStr);
More about date: http://docs.oracle.com/javase/7/docs/api/java/sql/Date.html
More about ResultSet : http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html
Instead of using
String myDateStr = rs.getString("MY_DATE")
you should use
Timestamp timestamp = rs.getTimestamp("MY_DATE");
JDBC / Database will handle the date transformation for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With