Java Convert OffsetDateTime to SQL Date
Tags: Java OffsetDateTime Java SQL Date Java 8
In this Java core tutorial we learn how to convert a java.time.OffsetDateTime object to a java.sql.Date object in Java programming language.
How to convert OffsetDateTime to SQL Date in Java
In Java, with a given OffsetDateTime object we can use follow the steps to convert it to a SQL Date object.
- Step 1: using the OffsetDateTime.toLocalDate() method to convert the OffsetDateTime object to LocalDate object.
- 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.
ConvertOffsetDatetimeToSQLDateExample1.java
import java.sql.Date;
import java.time.LocalDate;
import java.time.OffsetDateTime;
public class ConvertOffsetDatetimeToSQLDateExample1 {
public static void main(String... args) {
OffsetDateTime offsetDateTime = OffsetDateTime.now();
// Convert OffsetDateTime object to SQL Date object
LocalDate localDate = offsetDateTime.toLocalDate();
Date date = Date.valueOf(localDate);
System.out.println("OffsetDateTime: " + offsetDateTime);
System.out.println("SQL Date: " + date);
}
}
OffsetDateTime: 2022-05-23T12:38:45.360057900+07:00
SQL Date: 2022-05-23
Happy Coding 😊
Related Articles
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 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