The basic difference is java.util.Date represents both Date and Time information, whereas java.sql.Date represents only Date.
java.sql.Date is used only in JDBC to identify SQL DATE value.
This is the reason we can't map java.util.Date date to util java.sql.Date.
java.sql.Date: this class extends java.util.Date class and as per javadoc, it is just a thin wrapper around millisecond value which is used by JDBC to identify an SQL DATE type.
check below code snippet:
Output:
java.sql.Date: this class extends java.util.Date class and as per javadoc, it is just a thin wrapper around millisecond value which is used by JDBC to identify an SQL DATE type.
check below code snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class DateTimeExample { public static void main(String[] args) { //java.util.Date java.util.Date utilDate = new java.util.Date(); long dateTimeInMilliSeconds = utilDate.getTime(); System.out.println("utilDate: " + utilDate); System.out.println("dateTimeInMilliSeconds: " + dateTimeInMilliSeconds); //passing milliseconds to java.sql.Date constructor java.sql.Date sqlDate = new java.sql.Date(dateTimeInMilliSeconds); System.out.println("sqlDate: " + sqlDate); } } |
utilDate: Sat Feb 27 22:39:36 IST 2021 dateTimeInMilliSeconds: 1614445776759 sqlDate: 2021-02-27
Explanation: utilDate variable contains date and time both and when we pass the generated milliseconds to java.sql.Date object then only date is returned.
No comments:
Post a Comment