Convert java.sql.date to java.time.LocalDateTime

Working method:

public LocalDate convertToLocalDateViaMilisecond(Date dateToConvert) {
    return Instant.ofEpochMilli(dateToConvert.getTime())
      .atZone(ZoneId.systemDefault())
      .toLocalDate();
}

Non-working build for java.sql.Date (because it throws UnsupportedOperationException for instant() method):

public LocalDate convertToLocalDateViaInstant(Date dateToConvert) {
    return dateToConvert.toInstant()
      .atZone(ZoneId.systemDefault())
      .toLocalDate();
}

Credits to baeldung for this answer on his site:


It was actually easier than I thought. This worked for me:

//java.sql.ResultSet result
result.getTimestamp("value").toLocalDateTime()

Tags:

Java