The java.util.Date
class has a method called toInstant()
that converts the Date
instance to a java.time.Instant
.
The java.sql.Date
class extends the java.util.Date
class, but when I attempt to call toInstant()
on a java.sql.Date
, I receive an UnsupportedOperationException
.
Why is toInstant()
an unsupported operation on java.sql.Date
?
And what is the "correct" way to convert a java.sql.Date
to a java.time.Instant
?
sql. Date just represent DATE without time information while java. util. Date represents both Date and Time information.
The java. sql. Date represents the date value in JDBC. The constructor of this class accepts a long value representing the desired date and creates respective Date object.
The correct mapping between java.sql.Date
and java.time
is LocalDate
:
LocalDate date = sqlDate.toLocalDate();
If you really must, you can then derive an Instant
, although the extra information (time) will be arbitrary. For example:
Instant i = date.atStartOfDay(ZoneOffset.UTC).toInstant();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With