Differences between Java. util. Date and Java. SQL. Date and the difference and Application of Java. util. Date and Java. SQL. Date

Source: Internet
Author: User
  • Blog
  • Community
  • Homepage
  • New essay
  • Contact
  • Management
  • Subscription
Post-42-0 comments-72 differences between java. util. Date and java. SQL. Date and the application of java. util. Date are used in addition to SQL statements.
Java. SQL. Date is used for SQL statements. It contains only the Date but not the time part.
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 2nd parameter of setDate () of PreparedStament and the 2nd parameter of getDate () method of ResultSet are both java. SQL. Date
Conversion is
Java. SQL. Date date = new Java. SQL. Date ();
Java. util. Date d = new java. util. Date (date. getTime ());
The opposite is true.

--------------------------------------------------------------------------------
Inheritance relationship: java. lang. Object -- java. util. Date -- java. SQL. Date
The specific conversion relationship is java. util. Date d = new java. util. Date (new Java. SQL. Date ());

--------------------------------------------------------------------------------
SQL. date, usually in the database time field, util. date is generally a daily date Field

--------------------------------------------------------------------------------
Java. SQL. Date is mainly used in SQL!
Java. util. Date can be used in general environments!

--------------------------------------------------------------------------------

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 ());
System. out. println (utilDate. getYear ());

All the time and date can be formatted by SimpleDateFormat format ()

F. format (stp); f. format (sTime); f. format (sqlDate); f. format (utilDate)

Java. SQL. Date sqlDate = java. SQL. Date. valueOf ("2005-12-12 ");

UtilDate = new java. util. Date (sqlDate. getTime ());

Bytes --------------------------------------------------------------------------------------------------

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 );

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.
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 2nd parameter of setDate () of PreparedStament and the 2nd parameter of getDate () method of ResultSet are both java. SQL. Date
Conversion is
Java. SQL. Date date = new Java. SQL. Date ();
Java. util. Date d = new java. util. Date (date. getTime ());
The opposite is true.

--------------------------------------------------------------------------------
Inheritance relationship: java. lang. Object -- java. util. Date -- java. SQL. Date
The specific conversion relationship is java. util. Date d = new java. util. Date (new Java. SQL. Date ());

--------------------------------------------------------------------------------
SQL. date, usually in the database time field, util. date is generally a daily date Field

--------------------------------------------------------------------------------
Java. SQL. Date is mainly used in SQL!
Java. util. Date can be used in general environments!

--------------------------------------------------------------------------------

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 ());
System. out. println (utilDate. getYear ());

All the time and date can be formatted by SimpleDateFormat format ()

F. format (stp); f. format (sTime); f. format (sqlDate); f. format (utilDate)

Java. SQL. Date sqlDate = java. SQL. Date. valueOf ("2005-12-12 ");

UtilDate = new java. util. Date (sqlDate. getTime ());

Bytes --------------------------------------------------------------------------------------------------

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 );

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.