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

You oft need to convert java.util.Date to java.sql.Date if yous are storing dates inwards database e.g. SQL SERVER or MySQL. Since JDBC has their ain information types for appointment as well as fourth dimension e.g. java.sql.Date, java.sql.Time as well as java.sql.TimeStamp to jibe amongst database date, fourth dimension as well as date-time types, yous cannot transcend a java.util.Date directly. All methods which are suppose to shop dates e.g. setDate(paramName, paramValue) expects java.sql.Date, as well as therefore 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 as well as all it does is suppress or take away time-related fields from java.util.Date.


It is too a proficient instance of a shape which violates Liskov commutation principle, because fifty-fifty though java.sql.Date extends java.util.Date, yous cannot transcend some it to the method which aspect java.util.Date because all time-related methods e.g. getHour(), getMinute() as well as getSeconds() method throws java.util.NotSupportedException

I actually promise that JDBC driver could exercise the translation depending upon the information type of columns as well as Java developer could simply transcend 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 as well as 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 shape to facilitate conversion betwixt util appointment as well as SQL appointment but yous tin purpose getTime() method to extract long millisecond value from java.util.Date as well as exercise 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 as well as correct means to convert a java.util.Date to java.sql.Date inwards Java. To larn to a greater extent than nigh JDBC as well as how to write Java application which connects to the database, delight encounter Core Java, Volume II--Advanced Features (10th Edition), ane of the best books to larn advanced features of Java programming language.

 Since JDBC has their ain information types for appointment as well as 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 as well as after 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 as well as java.sql.Date past times using getTime() method.

4) Here is mapping betwixt MySQL appointment as well as fourth dimension types as well as Java appointment types:

 Since JDBC has their ain information types for appointment as well as 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 as well as JDBC. It's ane of the most useful tips piece working inwards JDBC. You tin fifty-fifty exercise a shape similar JdbcUtil to host all appointment as well as time-related conversion methods e.g. toSQLDate(), toSQLTime() etc.

Further Learning
Complete Java Masterclass
tutorial)
  • How exercise yous 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 as well as 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 2 dates inwards Java? (program)
  • How to add together days, months as well as years from a Date inwards Java? (solution)
  • The divergence betwixt java.sql.Time, java.sql.Timestamp as well as java.util.Date inwards Java? (answer)
  • How to increment 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 yous non purpose SimpleDateFormat inwards Java? (answer)


  • Sumber https://javarevisited.blogspot.com/

    0 Response to "How To Convert Java.Util.Date To Java.Sql.Date - Jdbc Example"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel