How To Convert Java.Util.Date To Java.Sql.Date - Jdbc Example

You ofttimes require 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 appointment 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 locomote past times 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 live on surprised to know that java.sql.Date is a subclass of java.util.Date together with all it does is suppress or take away time-related fields from java.util.Date.


It is besides a proficient event of a aeroplane which violates Liskov commutation principle, because fifty-fifty though java.sql.Date extends java.util.Date, you lot cannot locomote past times some it to the method which human face 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 but locomote past times 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, in that place is no method similar toSQLDate() inwards java.util.Date aeroplane to facilitate conversion betwixt util appointment together with SQL appointment but you lot tin operate 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 acquire to a greater extent than nigh JDBC together with how to write Java application which connects to the database, delight run across Core Java, Volume II--Advanced Features (10th Edition), 1 of the best books to acquire advanced features of Java programming language.

 Since JDBC has their ain information types for appointment together with fourth dimension e How to convert java.util.Date to java.sql.Date - JDBC Example





Things to remember

1) java.sql.Date mapped to DATE datatype on JDBC i.e. Type.Date, which corresponds to appointment equivalent inwards DB side. In SQLSERVER till 2008 it maps to DATETIME together with later 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 appointment together with fourth dimension types together with Java appointment types:

 Since JDBC has their ain information types for appointment together with fourth dimension e How to convert java.util.Date to java.sql.Date - JDBC Example



That's all nigh 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 aeroplane similar JdbcUtil to host all appointment 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 ii 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 appointment amongst Timezone inwards Java? (example)
  • 3 ways to compare ii 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 growth the appointment 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 operate SimpleDateFormat inwards Java? (answer)

  • Komentar

    Postingan populer dari blog ini

    Common Multi-Threading Mistakes Inwards Coffee - Calling Run() Instead Of Start()

    3 Examples Of Parsing Html File Inwards Coffee Using Jsoup

    Why You Lot Should Command Visibility Of Shape Too Interface Inward Java