Conversion between Java. util. Date and Java. SQL. Date

Source: Internet
Author: User
Tags dateformat

This problem is summarized as follows:

Java. SQL. date is a subclass of Java. util. date. A thin package that encapsulates millisecond values allows JDBC to mark millisecond values as SQL date values. To be consistent with the definition of SQL date. SQL. the millisecond value encapsulated by the date instance must be specified by setting the time, minute, second, and millisecond to zero in a specific time zone related to the instance.

The corresponding methods in Java. util. date have been discarded. You should use the calendar class to replace the date and time fields, and use the dateformat class to format and analyze the date string.

Installation and replacement between them: Java. utl. date is used in addition to SQL statements. SQL. date is used for SQL statements. It only contains the date but does not have time. It has the gettime method that returns the number of milliseconds. Naturally, you can directly build Java. util. date d = new Java. util. date (SQL. date. gettime (); Java. SQL. date = new Java, SQL. date (); Java. util. date d = new Java. util. date (date. gettime ());

The alternative method is to get the year, month, and day:
Import java. Text. simpledateformat;
Import java. util .*;
Java. util. 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.