You oftentimes quest to convert java.util.Date to java.sql.Date if you lot are storing dates inwards database e.g. SQL SERVER or MySQL. Since JDBC has their ain information types for engagement together with fourth dimension e.g. java.sql.Date, java.sql.Time together with java.sql.TimeStamp to check amongst database date, fourth dimension together with date-time types, you lot cannot overstep a java.util.Date directly. All methods which are suppose to shop dates e.g. setDate(paramName, paramValue) expects java.sql.Date, together with then it becomes essential to know how to convert java.util.Date to java.sql.Date inwards JDBC. You would survive surprised to know that java.sql.Date is a subclass of java.util.Date together with all it does is suppress or take time-related fields from java.util.Date.
It is likewise a skillful instance of a degree which violates Liskov commutation principle, because fifty-fifty though java.sql.Date extends java.util.Date, you lot cannot overstep around it to the method which await java.util.Date because all time-related methods e.g. getHour(), getMinute() together with getSeconds() method throws java.util.NotSupportedException.
I actually promise that JDBC driver could practise the translation depending upon the information type of columns together with Java developer could merely overstep the java.util.Date e.g. convert it to java.sql.Date if the column inwards the tabular array is of type DATE, java.sql.Time if the type of column is TIME together with java.sql.Timestamp if the information type of column is DATETIME.
This is the easiest together with correct means to convert a java.util.Date to java.sql.Date inwards Java. To larn to a greater extent than most JDBC together with how to write Java application which connects to the database, delight come across Core Java, Volume II--Advanced Features (10th Edition), 1 of the best books to larn advanced features of Java programming language.
Things to remember
1) java.sql.Date mapped to DATE datatype on JDBC i.e. Type.Date, which corresponds to engagement equivalent inwards DB side. In SQLSERVER till 2008 it maps to DATETIME together with later on maps to DATE information type.
2) java.sql.Date extends java.util.Date but voilates Liskov Substituion Principle.
3) You tin convert betwixt java.util.Date together with java.sql.Date past times using getTime() method.
4) Here is mapping betwixt MySQL engagement together with fourth dimension types together with Java engagement types:
That's all most how to convert java.util.Date to java.sql.Date inwards Java together with JDBC. It's 1 of the most useful tips piece working inwards JDBC. You tin fifty-fifty practise a degree similar JdbcUtil to host all engagement together with time-related conversion methods e.g. toSQLDate(), toSQLTime() etc.
Further Learning
Complete Java Masterclass
tutorial)How practise you lot calculate the divergence betwixt 2 dates inwards Java? (solution) How to convert a String to Date inwards Java? (example) How to acquire a day, calendar month together with twelvemonth from a Date inwards Java? (solution) How to convert XMLGregorianCalendar to Date inwards Java? (solution) How to acquire the current engagement amongst Timezone inwards Java? (example) 3 ways to compare 2 dates inwards Java? (program) How to add together days, months together with years from a Date inwards Java? (solution) The divergence betwixt java.sql.Time, java.sql.Timestamp together with java.util.Date inwards Java? (answer) How to increment the engagement inwards Java? (solution) How to convert local fourth dimension to GMT inwards Java? (answer) How to convert Date to String inwards Java? (answer) Why should you lot non piece of job SimpleDateFormat inwards Java? (answer)
It is likewise a skillful instance of a degree which violates Liskov commutation principle, because fifty-fifty though java.sql.Date extends java.util.Date, you lot cannot overstep around it to the method which await java.util.Date because all time-related methods e.g. getHour(), getMinute() together with getSeconds() method throws java.util.NotSupportedException.
I actually promise that JDBC driver could practise the translation depending upon the information type of columns together with Java developer could merely overstep the java.util.Date e.g. convert it to java.sql.Date if the column inwards the tabular array is of type DATE, java.sql.Time if the type of column is TIME together with java.sql.Timestamp if the information type of column is DATETIME.
Converting java.util.Date to java.sql.Date - Example
Unfortunately, at that topographic point is no method similar toSQLDate() inwards java.util.Date degree to facilitate conversion betwixt util engagement together with SQL engagement but you lot tin piece of job getTime() method to extract long millisecond value from java.util.Date together with practise a novel java.sql.Date based on that value equally shown below:Date at nowadays = new Date(); java.sql.Date sqlDate = new java.sql.Date(now.getTime());
This is the easiest together with correct means to convert a java.util.Date to java.sql.Date inwards Java. To larn to a greater extent than most JDBC together with how to write Java application which connects to the database, delight come across Core Java, Volume II--Advanced Features (10th Edition), 1 of the best books to larn advanced features of Java programming language.
Things to remember
1) java.sql.Date mapped to DATE datatype on JDBC i.e. Type.Date, which corresponds to engagement equivalent inwards DB side. In SQLSERVER till 2008 it maps to DATETIME together with later on maps to DATE information type.
2) java.sql.Date extends java.util.Date but voilates Liskov Substituion Principle.
3) You tin convert betwixt java.util.Date together with java.sql.Date past times using getTime() method.
4) Here is mapping betwixt MySQL engagement together with fourth dimension types together with Java engagement types:
That's all most how to convert java.util.Date to java.sql.Date inwards Java together with JDBC. It's 1 of the most useful tips piece working inwards JDBC. You tin fifty-fifty practise a degree similar JdbcUtil to host all engagement together with time-related conversion methods e.g. toSQLDate(), toSQLTime() etc.
Further Learning
Complete Java Masterclass
tutorial)