I'm trying to get a date from a DataObject (Service Date Object (SDO)) that comes to me as an input and insert it into an Oracle database. The problem has been that the Date I get does not seem to have the introduced hour.
I am using the setDate() method from DataObject with the following value: 2019-05-22T13:30:00Z.
For some reason, when using getDate() what is returning is the day entered with the hour set at 0 (2019-05-22 00:00:00).
I'm not sure if it's due to the input format or something related to the Date class from java.utils.
An easy solution would be to pass it as String and convert it into Date using a format but I would like to save this intermediate step.
java.util.Dateversusjava.sql.DateYour Question does not provide enough detail to know for sure, but I can take an educated guess.
I suspect your code calling
setDateand/orgetDateis using ajava.sql.Dateobject rather than ajava.util.Dateobject.➥ Check your
importstatements. If you used the wrong class by accident, that would explain the time-of-day getting set to 00:00.java.util.Daterepresents a moment in UTC (a date, a time-of-day, and an offset-from-UTC of zero hours-minutes-seconds).java.sql.Datepretends to represent a date-only, without a time-of-day and without a time zone or offset-from-UTC. Actually does contain a time-of-day and offset, but tries to adjust the time to 00:00:00.0 as part of the pretense.Confusing? Yes. These old date-time classes from the earliest days of Java are a bloody awful mess, built by people who did not understand the complexities of date-time handling. Avoid these legacy date-time classes!
These legacy classes were supplanted years ago by the modern java.time classes defined in JSR 310. Try to do all your work in java.time. When interoperating with old code such as SDO that is not yet updated for java.time, call on new conversion methods added to the old classes.
The modern replacement of a
java.util.Dateisjava.time.Instant. Both represents a moment in UTC, thoughInstanthas a finer resolution of nanoseconds versus milliseconds.Convert from modern class to legacy class. Beware of data-loss: Any microseconds or nanoseconds in the fractional second are truncated to milliseconds (as noted above).
Pass to your SDO object.
Going the other direction, retrieve the legacy
java.util.Dateobject and immediately convert to anInstant.To see that same moment through the wall-clock time used by the people of a particular region (a time zone), apply a
ZoneIdto get aZonedDateTimeobject.No! Use smart objects, not dumb strings. We have the industry-leading date-time library built into Java, so use it.
Database
As of JDBC 4.2, we can directly exchange java.time objects with the database.
Your JDBC driver may optionally handle
Instant. If not, convert toOffsetDateTime.Retrieval.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date,Calendar, &SimpleDateFormat.To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*classes.Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval,YearWeek,YearQuarter, and more.