I have a java.sql.Date instance and I want to use the java.util.Date.toInstant() method instead of java.sql.Date.toInstant(), for example, is it possible?
-
3`import` the class that you want to use. What else is the question about? – Naman Jul 10 '20 at 03:54
-
Why not extend java.sql.Date and override it with the impl of java.util? There are ethical concerns about this, though. – Adrian M. Jul 10 '20 at 04:07
-
@Naman This is not up to me, the JDBC driver returns java.sql.Date. – Philippe Gioseffi Jul 10 '20 at 04:12
-
@AdrianM. I don't know if this would help because the JDBC driver is the one responsible and it returns java.sql.Date. – Philippe Gioseffi Jul 10 '20 at 04:13
-
4It certainly is up to you! Since JDBC 4.2 instruct the JDBC driver to return `LocalDate` and forget everything about the two poorly and confusingly desiigned `Date` classes. See [Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2](https://stackoverflow.com/questions/43039614/insert-fetch-java-time-localdate-objects-to-from-an-sql-database-such-as-h2). – Ole V.V. Jul 10 '20 at 04:13
-
@OleV.V., is it safe to replace java.util.Date to java.time.LocalDate in old classes? – Philippe Gioseffi Jul 10 '20 at 04:19
-
Define "safe". (It depends on how the old classes use `Date`. You will probably need to change code in the old classes to make it work. How much code ... depends on the classes.) – Stephen C Jul 10 '20 at 04:31
-
2But to answer your question: it is not possible for your code that uses a `java.sql.Date` to call an method in `java.util.Date` that `java.sql.Date` overrides. The Java language doesn't allow it. You can't even do it reflectively. – Stephen C Jul 10 '20 at 04:35
-
@StephenC when I used the word "safe" I meant if changing java.util.Date to java.time.LocalDate would maintain at least the JDBC layer acting the same way. – Philippe Gioseffi Jul 10 '20 at 04:55
-
@StephenC and thanks, you actually answered my original question without focusing in the example I provided, even thought is a real scenario. Your second comment should be the accpeted answer if you provide one. – Philippe Gioseffi Jul 10 '20 at 04:56
-
Regarding "safe", I am pretty sure that the answer is "yes" for that meaning of safe. (Not 100% ... because I have not actually tried it.) – Stephen C Jul 10 '20 at 05:54
-
@StephenC I'll try it and get back at you with an answer in these comments. – Philippe Gioseffi Jul 10 '20 at 05:57
2 Answers
java.time and JDBC 4.2
I recommend that you stick to java.time, the modern Java date and time API to which Instant
belongs and forget about the two Date
classes mentioned in the question. They are both poorly designed and both long outdated. Since JDBC 4.2 we can retrieve java.time types from a ResultSet
.
I am assuming that your SQL query is returning SQL datatype date
.
PreparedStatement stmt = yourDatabaseConnection
.prepareStatement("select your_date_column from your_table;");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
LocalDate date = rs.getObject("your_date_column", LocalDate.class);
// do something with date
}
You want to convert the date to an Instant
? That conversion doesn’t readily make sense. An SQL date
usually is a calendar date defined by year, month and day of month, without time zone. An Instant
is a point in time, a completely different beast. If you can decide on a time of day and a time zone to use, a conversion is possible. One option is:
Instant inst = date.atStartOfDay(ZoneId.systemDefault()).toInstant();
If you cannot avoid getting a java.sql.Date
Having a java.sql.Date
, again we need a time of day and a time zone if we want to convert to Instant
. There is a reason why java.sql.Date.toInstant()
unconditionally throws UnsupportedOperationException
(which I guess caused you to ask the question). I would convert the Date
to a LocalDate
and then proceed as before:
java.sql.Date oldfashionedSqlDate = getFromSomewhere();
Instant inst = oldfashionedSqlDate.toLocalDate()
.atStartOfDay(ZoneId.systemDefault())
.toInstant();
A shorter but more low-level and cryptic alternative is:
Instant inst = Instant.ofEpochMilli(oldfashionedSqlDate.getTime());
The two ways may not always give the same result, which should be your first guidance for choosing. In case the Date
contrary to the specification holds a time of day other than the start of day, the latter method will give you that time, whereas the former will give you the start of the day as the code says. BTW the latter is what java.util.Date.toInstant()
does.
In general is it possible to circumvent the method implementation in the subclass and call the superclass method directly?
Is it possible to choose which overridden method to use in Java?
No, that is not possible in the language. Java doesn’t offer any syntax for such a trick. Holger’s comment under this answer seems to suggest that it is possible through reflection under some circumstances such as you being able to open the java.base
module. See the last link at the bottom for more details.
Links
- Related question: Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2
- Oracle tutorial: Date Time explaining how to use java.time.
- Related question: I want to print hi GrandFather;but it seems to print hi father

- 81,772
- 15
- 137
- 161
-
Great answer for the example I provided, but can you extend your answer and provide a explanation to the question `Is it possible to choose which overridden method to use in Java?` in a more general way without getting too attached to the example? – Philippe Gioseffi Jul 10 '20 at 05:08
-
2To be precise, [it’s not entirely impossible to invoke a super method](https://stackoverflow.com/q/60322808/2711488) skipping the most specific. However, that requires private access to the particular class, which is only possible within the same module or to an open(ed) module. Since the class of this question belongs to `java.base`, the caller would need to open it, which is currently possible, but that door might be closed in future versions, only allowed for particular debugging or instrumentation facilities. – Holger Jul 10 '20 at 08:31
To answer your question as asked:
It is not possible for your code that uses a java.sql.Date
to call an method in java.util.Date
which java.sql.Date
overrides.
That would break data abstraction, and the Java language doesn't allow it. You can't even do it reflectively.
(As other answers and comments indicate, there are alternative approaches that avoid this problem. Some more practical than others1.)
1 - The idea of creating a custom subtype of java.sql.Date
is not practical. You don't want to be modifying JDBC drivers to try to solve this.

- 698,415
- 94
- 811
- 1,216
-
Create a custom subtype of java.sql.Date is an idea that never crossed my mind. Thanks for your answer, it answered what I was looking for. – Philippe Gioseffi Jul 10 '20 at 06:12