Java.util.Date and Java.sql.Date

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.