Java Convert SQL Date to LocalDateTime

Tags: Java SQL Date Java LocalDateTime Java 8

In this Java core tutorial we learn how to convert a java.sql.Date object to a java.time.LocalDateTime object in Java programming language.

How to convert SQL Date to LocalDateTime in Java

In Java, with a given SQL Date object we can use follow the steps to convert it to a LocalDateTime object.

  • Step 1: using the Date.toLocalDate() method to convert the SQL Date object to LocalDate object.
  • Step 2: using the LocalDate.atStartOfDay() method to convert the LocalDate object from step 1 to a LocalDateTime object.

ConvertSQLDateToLocalDateTimeExample1.java

import java.sql.Date;
import java.time.LocalDate;
import java.time.LocalDateTime;

public class ConvertSQLDateToLocalDateTimeExample1 {
    public static void main(String... args) {
        Date date = new Date(System.currentTimeMillis());

        // Convert SQL Date object to LocalDateTime object
        LocalDate localDate = date.toLocalDate();
        LocalDateTime localDateTime = localDate.atStartOfDay();

        System.out.println("SQL Date: " + date);
        System.out.println("LocalDateTime: " + localDateTime);
    }
}
The output as below.
SQL Date: 2022-05-23
LocalDateTime: 2022-05-23T00:00

Happy Coding 😊

Java Convert LocalDate to SQL Date

Java Convert Instant to SQL Date

Java Convert LocalDateTime to SQL Date

Java Convert ZonedDateTime to SQL Date

Java Convert OffsetDateTime to SQL Date

Java Convert Date to SQL Date

Java Convert Calendar to SQL Date

Java Convert GregorianCalendar to SQL Date

Java Convert SQL Date to LocalDate

Java Convert SQL Date to ZonedDateTime

Java Convert SQL Date to OffsetDateTime

Java Convert SQL Date to Calendar

Java Convert SQL Date to GregorianCalendar

Java Convert SQL Date to Instant