I know that a java.sql.Date should have hours, minutes, seconds and milliseconds set to zero, to comply with the definition of standard SQL date. This is documented here (the same in Java 8).
I also know that the Oracle DATE type does have these time fields of YEAR, MONTH, DAY, HOUR, MINUTE, and SECOND. But no fractional second nor time zone.
I noticed that the same query in Java 6 and in Java 8 does not behave the same :
private static final String REQUETE_LISTE_CALENDRIER_DATE =
" SELECT ID_DATE, JOUR" +
" FROM CALENDRIER " +
" WHERE ID_DATE = ? ";
Binding to the PreparedStatement
a java.sql.Date "dateCourante" defined like this (which sets a value to those time fields) :
GregorianCalendar gregorianCalendar = new GregorianCalendar(); // "now"
java.sql.Date dateCourante = new java.sql.Date(gregorianCalendar.getTime().getTime()); // date AND time of "now"
- with Java 6, I find a value,
- with Java 8, I do not.
In my database, the date has hours, minutes, seconds to zero. We can check with the following query :
select to_char(id_date, 'DD/MM/YYYY HH24:MI:SS')
from calendrier
where id_date = to_date('26/08/2016', 'DD/MM/YYYY');
that gives :
26/08/2016 00:00:00
So, what I understand, is that :
- in Java 6, the time fields of a
java.sql.Date
are set to zero before the query is launched on the database, whereas - in Java 8, the time fields of
java.sql.Date
are left as is in the query.
I have not been able to find documentation about this behavior.
Can anybody confirm or explain that ?
As a workaround, I use this, as explained here : dDate = java.sql.Date.valueOf(dDate.toLocalDate()); // where dDate is java.sql.Date