Java Convert Instant to SQL Date
Tags: Java Instant Java SQL Date Java 8
In this Java core tutorial we learn how to convert a java.time.Instant object to a java.sql.Date object in Java programming language.
How to convert Instant to SQL Date in Java
In Java, with a given Instant object we can use follow the steps to convert it to a SQL Date object.
- Step 1: using the LocalDate.ofInstant(Instant instant, ZoneId zone) method to convert the Instant object to LocalDate object in default system time zone.
- Step 2: using the java.sql.Date.valueOf(LocalDate date) static method to instantiate a new SQL Date object from LocalDate value at step 1.
ConvertInstantToSQLDateExample1.java
import java.sql.Date;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
public class ConvertInstantToSQLDateExample1 {
public static void main(String... args) {
Instant instant = Instant.now();
// Convert Instant object to SQL Date object
LocalDate localDate = LocalDate.ofInstant(instant, ZoneId.systemDefault());
Date date = Date.valueOf(localDate);
System.out.println("Instant: " + instant);
System.out.println("Date: " + date);
}
}
Instant: 2022-05-22T18:17:53.309197400Z
Date: 2022-05-23
Happy Coding 😊
Related Articles
Java Convert LocalDate to SQL Date
Java Convert LocalDateTime to SQL Date
Java Convert ZonedDateTime to SQL Date
Java Convert OffsetDateTime 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
Java Convert SQL Date to ZonedDateTime
Java Convert SQL Date to OffsetDateTime
Java Convert SQL Date to Calendar