Java Convert SQL Date to ZonedDateTime

Tags: Java SQL Date Java ZonedDateTime Java 8

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

How to convert SQL Date to ZonedDateTime in Java

In Java, with a given SQL Date object we can follow these steps to convert it to a ZonedDateTime object.

  • Step 1: use Date.toLocalDate() method to convert the SQL Date object to a LocalDate object.
  • Step 2: use LocalDate.atStartOfDay(ZoneId zone) method to convert the LocalDate object from step 1 to a ZonedDateTime object with system default time zone.

ConvertSQLDateToZonedDateTimeExample1.java

import java.sql.Date;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;

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

        // Convert SQL Date object to ZonedDateTime object
        LocalDate localDate = date.toLocalDate();
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());

        System.out.println("SQL Date: " + date);
        System.out.println("ZonedDateTime: " + zonedDateTime);
    }
}
The output as below.
SQL Date: 2022-05-23
ZonedDateTime: 2022-05-23T00:00+07:00[Asia/Bangkok]

Happy Coding 😊

Java Convert SQL Date to OffsetDateTime

Java Convert SQL Date to Calendar

Java Convert SQL Date to GregorianCalendar

Java Convert SQL Date to Instant

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 LocalDateTime