Java.util.date and Java.sql.date
Java.util.date is the parent class of Java.sql.date, where java.util.date is used anywhere except the database. Both have gettime methods (get a long type). So we can convert each other.
java.sql.date date_sql = new java.sql.date(System.currentTime());java.util.date date = new java,util.date(date_sql.getTime());
The reverse conversion is the same.
Another type of conversion:
java.util.date date = new java.util.date(date_sql);//用子类创建父类
Preparement in GetDate () and setdate (Datesql) are both returned and passed in Java.sql.date, so convert first
Java.sql.date has no parameterless constructor, must pass value, Java.util.date can use the parameterless constructor, gets the current time
Java.sql.date time format is year-month-day, no time part.
Forced to get from datesql seconds, and throws an exception.
But does not mean that there is no time part of the precision.
datesql.getTime() == date.getTime() //true
Two objects can be formatted, using Class SimpleDateFormat
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");java.util.Date date = new java.util.Date();System.out.println(simpleDateFormat.formate(date));java.sql.Date dateSql = new java.sql.Date(date.getTime());System.out.println(simpleDateFormat.formate(dateSql)); // 通过format转换后可以显示时分秒
Note: Java.sql.date can be displayed by format after minutes and seconds, that is, when the accuracy of the minute and seconds is not lost
Java.util.Date and Java.sql.Date