Java Convert SQL Date to Instant

Tags: Java SQL Date Java Instant Java 8

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

How to convert SQL Date to Instant in Java

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

  • Step 1: use Date.getTime() method to get epoch milliseconds since January 1, 1970, 00:00:00 GMT value.
  • Step 2: use Instant.ofEpochMilli(long epochMilli) to create a new Instant object from epoch milliseconds value of step 1.

ConvertSQLDateToInstantExample1.java

import java.sql.Date;
import java.time.Instant;

public class ConvertSQLDateToInstantExample1 {
    public static void main(String... args) {
        Date date = Date.valueOf("2022-05-29");

        // Convert SQL Date object to Instant object
        Instant instant = Instant.ofEpochMilli(date.getTime());

        System.out.println("SQL Date: " + date);
        System.out.println("Instant: " + instant);
    }
}
The output as below.
SQL Date: 2022-05-29
Instant: 2022-05-28T17:00:00Z

Happy Coding 😊

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 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