4

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,

Community
  • 1
  • 1
Barney
  • 2,786
  • 2
  • 32
  • 34
  • Why you need to read date as a string?? Do you want any computation over date later?? Like compare, substring etc. – ravibagul91 Jan 14 '14 at 01:27
  • 1
    You may have missed the point of the answers on the other question… The point is that your question does not make sense. You should be asking for a java.sql.Date object via the [`getDate`](http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getDate(int)) method, not asking for a string. The very purpose of a JDBC driver is to mediate between the database engine and your Java app, to transform natively defined values into common Java objects. For date-time values, every JDBC driver's job is to give you java.sql.Date objects. From there you can create any string rendering you want. – Basil Bourque Jan 14 '14 at 01:29
  • possible duplicate of [Default JDBC date format when reading date as a string from ResultSet](http://stackoverflow.com/questions/14700962/default-jdbc-date-format-when-reading-date-as-a-string-from-resultset) – Mark Rotteveel Jan 14 '14 at 11:33
  • 1
    @Basil, thank you, but I feel it may be you who is missing the point, and possibly making invalid assumptions about what I am trying to achieve. The question makes perfect sense: somewhere there is some code which converts a database date value to string; I simply wish to know how it determines which format to use, nothing more, nothing less. – Barney Jan 14 '14 at 20:06
  • 1
    not what you want to hear, but seems like there should be a link to http://stackoverflow.com/q/19840542/217324 – Nathan Hughes Jan 14 '14 at 20:47
  • @Nathan: Thanks, that is helpful, as it suggests it may be hard-coded in the driver, and not dependent on the environment. – Barney Jan 14 '14 at 20:51

3 Answers3

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.

Barney
  • 2,786
  • 2
  • 32
  • 34
0

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.

0

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.

tonga
  • 11,749
  • 25
  • 75
  • 96