Java. util. Date java. SQL. Date java. SQL. Timestamp

Source: Internet
Author: User
Tags dateformat
Sorting 1:
I personally think this article is very detailed. For more information about the use of Java. SQL. Date, see.
Java. SQL. date only stores date data, not time data
// Time data will be lost
Preparedstatement. setdate (1, new java. SQL. date (date. gettime ()));
// It can be handled in this way
Preparedstatement. settimestamp (1, new java. SQL. timestamp (New java. util. Date (). gettime ()));

// You can obtain the complete data, including the date and time.
Java. util. Date d = resultset. gettimestamp (1 );
// This method is more suitable to avoid some potential timestamp problems.
Java. util. Date d = new java. util. Date (resultset. gettimestamp (1). gettime ());
Self-complementing
In this case:
When storing data in the database, you can receive the java. util. date type and use the gettime () method to obtain the long value representing the date object. Then, you can use this long value to construct a timestamp object and store it in the database.
When getting data from the database, you can first get Timestamp and use its gettime () method to get the long value, and then construct a Java with this long value. util. date object. Let's just say New simpletimeformat ("yyyyy-mm-dd hh: mm: SS"). Format () and so on.

Sorting 2:
It is very convenient to use timestamp to record the date and time, but sometimes it does not need the millisecond after the decimal point, so you need to redefine the format when converting to string.
Timestamp is converted to string: simpledateformat df = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); // defines the format, do Not Display millisecond timestamp now = new timestamp (system. currenttimemillis (); // obtain the current system time string STR = DF. format (now); string to timestamp: simpledateformat df = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
String time = DF. format (new date (); timestamp Ts = timestamp. valueof (time); Arrangement 3: In resultset, the Data Type of setdate or getdate that we often use is Java. SQL. date, but in Java programs, we are generally used to Java. util. date. Therefore, in the DaO layer, we often encounter the mutual conversion of these two data types. After one afternoon's tossing, the monks made a small summary of the conversion methods of the two, I hope you will give me some advice.
Relationship between the two
Java. Lang. Object
|
+ --- Java. util. Date
|
+ ---- Java. SQL. Date
From this figure, we can know that Java. SQL. date is inherited from Java. util. Date.

Mutual Conversion
1. Use the gettime () function
Both classes provide the gettime () function, which is used to return the corresponding number of milliseconds (long type ). This function can be used for conversion:
Java. util. Date utildate = new java. util. Date (sqldate. gettime (); // SQL-> util
Java. SQL. Date sqldate = new java. SQL. Date (utildate. gettime (); // util-> SQL

2. Use the simpledateformat class for conversion.
Simpledateformat is a country-sensitive method to format and analyze data. It allows formatting (date-> text), syntax analysis (text-> date), and standardization.
Simpledateformat dateformat = new simpledateformate ("yyyy-mm-dd hh: mm: SS ");
Java. util. Date utildate = dateformat. parse (sqldate. tostring ());

3. Direct Conversion
Because Java. SQL. date is inherited from Java. util. date, you can directly use:
Utildate = sqldate;

4. Alternative methods for obtaining dates:
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 );

PS: in the Java. util. Date class, getyear () must be added with 1900 to obtain the actual value. In getmonth (), 1 label is added: Java date timestamp SQL time date.

 

For your security, please only open the URL with reliable source

Cancel website opening

From: http://hi.baidu.com/zhangyx/blog/item/5fdf0a4f1b9f573caec3ab52.html

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.