In this article, we will discuss how to convert java.util.Date to java.sql.Date and vice-versa
1. java.util.Date class :
- Date class introduced in Java 1.0 version
- Implements Serializable, Cloneable and Comparable interfaces
- To work with new Joda Date/Time API introduced in Java 1.8 version, it provides below methods
- from(Instant)
- toInstant()
- Since Java 1.2 version, Date class has compareTo() method to compare 2 Date objects
- If the comparison of date1.compareTo(date2) method returns,
- positive (1) then date1 is greater/future date when comparing with date2
- negative (-1) then date1 is lesser/past date when comparing with date2
- zero (0) then date1 is equal/same date when comparing with date2
2. java.sql.Date class :
- java.sql.Date class introduced in Java 1.1 version
- java.sql.Date is the sub–class of java.util.Date which is mainly used for JDBC database operations
- java.sql.Date class represents only Date part whereas java.util.Date represents both Date & Time parts starting from January 1, 1970 00:00:00.000 GMT in milliseconds
3. java.util.Date to java.sql.Date conversion :
- First, create instance of java.util.Date which returns current Date & Time along with Day
- To convert java.util.Date to java.sql.Date, instantiate java.sql.Date and pass above created java.util.Date as constructor–argument in milliseconds
ConvertJavaUtilDateToJavaSqlDate.java
package in.bench.resources.date.comparison;
import java.util.Date;
public class ConvertJavaUtilDateToJavaSqlDate {
public static void main(String[] args) {
// current date
Date date = new Date();
System.out.println("Current Util Date :- \n" + date);
// convert to java.sql.Date
java.sql.Date sqlDate = new java.sql.Date(date.getTime()); // constructor-arg
System.out.print("\nCurrent SQL Date :- \n" + sqlDate);
}
}
Output :
Current Util Date :-
Thu Jan 12 16:15:21 IST 2023
Current SQL Date :-
2023-01-12
4. java.sql.Date to java.util.Date conversion :
- First, get current time in milliseconds using System.currentTimeMillis();
- Instantiate java.sql.Date passing above obtained current time in milliseconds as constructor–argument
- To convert java.sql.Date to java.util.Date, instantiate java.util.Date and pass java.sql.Date in milliseconds using getTime() method as constructor–argument
ConvertJavaSqlDateToJavaUtilDate.java
package in.bench.resources.date.comparison;
import java.util.Date;
public class ConvertJavaSqlDateToJavaUtilDate {
public static void main(String[] args) {
// current time in milliseconds
long currentTimeMillis = System.currentTimeMillis(); // time in millis
// current SQL Date
java.sql.Date sqlDate = new java.sql.Date(currentTimeMillis); // constructor-arg
System.out.println("Current SQL Date :- \n" + sqlDate);
// current date
Date date = new Date(sqlDate.getTime());
System.out.print("\nCurrent Util Date :- \n" + date);
}
}
Output :
Current SQL Date :-
2023-01-12
Current Util Date :-
Thu Jan 12 16:18:44 IST 2023
Related Articles :
- Java 8 – How to get current Date in different ways ?
- Java 8 – How to convert java.util.Date to LocalDate and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to OffsetDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to Instant and vice-versa ?
- Java 8 – How to convert java.util.Date to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert java.util.Date to Calendar and vice-versa ?
- Java 8 – How to convert java.util.Date to GregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to XMLGregorianCalendar and vice-versa ?
- Java 9 – How to convert java.util.Date to LocalDate using ofInstant() method ?
- Java 9 – How to convert java.util.Date to LocalTime using ofInstant() method ?
- Java – How to convert java.util.Date to String in different formats ?
- Java – How to convert String to java.util.Date in different formats ?
- Java – How to get Date/time with AM/PM marker and Zone ?
- Java 8 – How to get all dates between two Dates ?
- Java – How to compare 2 Date instances using Date.compareTo() method ?
- Java – How to convert java.util.Date to java.sql.Date and vice-versa ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/sql/Date.html
Happy Coding !!
Happy Learning !!