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