Java Convert Date to SQL Date
Tags: Java Date Java SQL Date Java 8
In this Java core tutorial we learn how to convert a java.util.Date object to a java.sql.Date object in Java programming language.
How to convert Date to SQL Date in Java
In Java, with a given Date object we can use follow the steps to convert it to a SQL Date object.
- Step 1: using the Date.getTime() method to return number of milliseconds since January 1, 1970, 00:00:00 GMT.
- Step 2: using the java.sql.Date(long date) constructor to instantiate a new SQL Date object from milliseconds value of step 1.
ConvertDateToSQLDateExample1.java
import java.util.Date;
public class ConvertDateToSQLDateExample1 {
public static void main(String... args) {
Date date = new Date();
// Convert Date object to SQL Date object
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
System.out.println("Date: " + date);
System.out.println("SQL Date: " + sqlDate);
}
}
Date: Mon May 23 12:46:09 ICT 2022
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 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