Where is the date format specified when reading a

2020-03-24 05:10发布

问题:

This is exactly the same question as Default JDBC date format when reading date as a string from ResultSet, but that question was never actually answered as such; people just offered alternative ways to write the code.

I'm specifically asking if anyone knows where the default conversion format is specified, and if it is defined by a JDBC spec somewhere, or just left up to the individual JDBC driver. If the latter, then an additional question of how does Oracle 10g JDBC do this?

** Added 15-Jan:

Sorry, I thought the question was sufficiently clear, but apparently not. I am not trying to retrieve a date value from a database, and have no need for suggestions on ways to do that.

I do not think this is a duplicate of the referenced question, which is why I asked a separate question here. Whilst the title is the pretty much same, the referenced question (regardless of what the OP intended) has been answered with alternative approaches for retrieving a date value from a database, which is not what I wish to know.

Cheers,

回答1:

Ok, so tracing through the JDBC code, it looks like the Oracle driver hard-codes its own formatting:

  int k = (((bytes[0 + j] & 255) - 100) * 100 + (bytes[1 + j] & 255)) - 100;
  String s = k + "-" + toStr(bytes[2 + j])
      + "-" + toStr(bytes[3 + j])
      + " " + toStr(bytes[4 + j] - 1)
      + ":" + toStr(bytes[5 + j] - 1)
      + ":" + toStr(bytes[6 + j] - 1)
      + ".0";

So from this, I'm guessing that:

  1. This behaviour is not specified anywhere in any JDBC spec.
  2. Each driver library may return any format it likes.

I'm assuming these because different implementations return different formats. Of course, it is still possible that this format is mandated somewhere in a JDBC spec, and some implementations are non-conformant, but that seems unlikely. I certainly couldn't find any references to this in a JDBC spec (but that doesn't mean they are not there somewhere!).

However, the key point as far as I am concerned, is that the format is hard-coded in the library and not overridable by any environment or other settings, but also won't change across systems as long as the driver library version remains the same.



回答2:

Oracle 10g JDBC is closed source, so I think we'll never know :) but I bet it first asks the server for locale infomation and, according to the server settings, it adjusts the internal data format it will use. My 2c.



回答3:

I'm not 100% sure of all databases' implementations for SQL Date type. But from javadoc of Oracle database, it seems that Oracle DB's DATE can be easily converted to java.sql.Date. The same thing is true for MySQL and PostgreSQL: SQL DATE can be converted to java.sql.Date.



标签: java jdbc