Differences between java. util. Date and java. SQL. Date and Their Applications,

Source: Internet
Author: User

Differences between java. util. Date and java. SQL. Date and Their Applications,

Java. util. Date is used in addition to SQL statements.
Java. SQL. Date is used for SQL statements. It contains only the Date but not the time part.

Java. SQL. Date is a type corresponding to the database Date, while java. util. Date is a Date of pure java.

It has the getTime method that returns the number of milliseconds, and can be directly constructed.
Java. util. Date d = new java. util. Date (sqlDate. getTime ());

Java. util. Date is the parent class of java. SQL. Date (note spelling)
The former is a commonly used time class. We usually use it to format or obtain the current time.
The latter is used when reading and writing the database, because the setDate () parameter of PreparedStament is 2nd and the getDate () parameter of ResultSet is both java. SQL. Date

Conversion is
Java. SQL. Date date = new Java. SQL. Date ();
Java. util. Date sqlDate = new java. util. Date (date. getTime ());
The opposite is true.

Java. SQL. Date can only store dates, but cannot store time.

All time dates can be formatted by SimpleDateFormat format ()
SimpleDateFormat f = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss ");
Java. util. Date utilDate = new Date ();
Java. SQL. Date sqlDate = new java. SQL. Date (utilDate. getTime ());
Java. SQL. Time sTime = new java. SQL. Time (utilDate. getTime ());
Java. SQL. Timestamp stp = new java. SQL. Timestamp (utilDate. getTime ());
F. format (stp );
F. format (sTime );
F. format (sqlDate );
F. format (utilDate)
Java. SQL. Date sqlDate = java. SQL. Date. valueOf ("2005-12-12 ");

The alternative method is to get the year, month, and day:
Import java. text. SimpleDateFormat;
Import java. util .*;
Java. util. Date date = new java. util. Date ();
// If you want to obtain the format of YYYYMMDD
SimpleDateFormat sy1 = new SimpleDateFormat ("yyyyMMDD ");
String dateFormat = sy1.format (date );

// If You Want To separately obtain the year, month, and day

SimpleDateFormat sy = new SimpleDateFormat ("yyyy ");

SimpleDateFormat sm = new SimpleDateFormat ("MM ");

SimpleDateFormat sd = new SimpleDateFormat ("dd ");

String syear = sy. format (date );

String smon = sm. format (date );

String sday = sd. format (date );

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.