Differences between Java. util. Date and Java. SQL. Date

Source: Internet
Author: User
Tags time and date
Java. Util. Date And java. SQL. Date Differences

Root directory
Java.Util. DateIn addition to SQL statements
Java.SQL. DateIt is used for SQL statements. It only contains the date and no time.
It has the gettime method that returns the number of milliseconds, and can be directly constructed.
Java.Util. DateD = new Java.Util. Date(Sqldate. gettime ());
...

--------------------------------------------------------------------------------
JAVA. util. date is Java. SQL. date parent class (note spelling)
the former is a commonly used class that represents time, we usually use it to format or obtain the current time
when reading and writing the database after the latter, because the setdate () parameter of preparedstament and getdate () of resultset () the 2nd parameters of the method are all 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 the same

--------------------------------------------------------------------------------
Inheritance relationship: Java. Lang. Object -- "Java.Util. Date-- "Java.SQL. Date
The specific conversion relationship is Java.Util. DateD = new Java.Util. Date(New Java.SQL. Date());

--------------------------------------------------------------------------------
SQL. Date, Usually in the database time field,Util. DateGenerally, it is a daily Date Field.

--------------------------------------------------------------------------------
Java.SQL. DateIt is mainly used in SQL!
Java.Util. DateIn a normal environment!

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

Simpledateformat F = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
Java.Util. DateUtildate = new date ();
Java.SQL. DateSqldate = 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. DateSqldate = 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. DateDate = 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.