3

I'm wrestling with the issue of time zones across SQL, jdbc, JVM and linux.

I can see that the class I need to hand to / get from jdbc is java.sql.date. So I read the documentation, and can't make ANYTHING of it.

As I understand it, both java.util.Data and java.sql.date hold a number of milliseconds since the epoch.

When I look at the definition for the constructor for java.sql.date it says

"Constructs a Date object using the given milliseconds time value. If the given milliseconds value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT."

OK - question 1 - what can be the meaning of "If the given milliseconds value contains time information" - clearly it contains SOME time information !!!

Do they mean "if the number of milliseconds you give the routine doesn't represent an exact day boundary, taking into account leap seconds etc. etc " ?? That sounds a fairly random test to meet/fail !

Or do they mean " for Heaven's sake give us something near mid-day, and we'll truncate it to the beginning of the day in question ?

question 2 - what, in words, is this strange clause "FOR" ?? It is clearly behaviour that is intended to make some gears mesh properly - but what gears ARE they?

3 Answers 3

6

java.sql.Date, java.sql.Time and java.sql.Timestamp all extend java.util.Date, however:

  • java.sql.Date has milliseconds rounded to midnight - it's a "date with zero time"
  • java.sql.Time has milliseconds less than 24 hours - it's like a java.util.Date on 1970-01-01
  • java.sql.Timestamp has full Date and Time - like a regular java.util.Date
Sign up to request clarification or add additional context in comments.

2 Comments

On the face of it, your answer seems to contradict the published documentation. That says "the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT" I suppose that might mean that the value contained will always be midnight GMT on some day or other - though it's a pretty clumsy way to say it.
If it means to say " a java.sql.Date object will always contain midnight GMT on some day or other" then that can only be true to the number of leap-seconds which were known to the JVM at the moment the object was created. If the leap-second information is later updated the same moment can shift backwards into the previous day!!!
0

If I was you, I would avoid using the millisecond constructor. Instead, use Date.valueOf.

3 Comments

but java.sql.Date only has a milliseconds constructor - the other one is deprecated. Oh - I see - construct it using a zero milliseconds value, then use the valueOf method to install a moment into it - Hmmm that might allow me to cut through the cloud of doubt. I need to look up the spec of "JDBC date escape format"
OK - that fancy phrase seems to mean "representing a date in in the format yyyy-mm-dd " - that's easy enough. I assume that the result represents midnight in UTC - or is it midnight in the JVM default time zone I wonder.
Right - II see the static valueOf method is a pseudo-constructor... like instanceOf for other things. TVM
0

java.sql.Date represents only the date, without any time information in it. java.sql.Time should be used for the time.

1 Comment

Thanks Nivas - but I assume you must mean "only the date, in UTC, including such leap seconds as were known at the moment of creation" ???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.