Use simpledateformat and messageformat to insert the date formats of yyyy-mm-dd AND yyyy-mm-dd hh: mm: Ss to the database.

Source: Internet
Author: User

Simpledateformat datefm = new simpledateformat ("yyyy-mm-dd"); // format the current system date
QDs. setdate ("ticket creation date", date. valueof (datefm. Format (New java. util. Date ())));

In this case, the database field is of the date type.

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

String ticket creation date = messageformat. format ("{0, date, yyyy-mm-dd hh: mm: SS}", new object [] {New Java. SQL. date (system. currenttimemillis ())});
QDs. settimestamp ("ticket date", timestamp. valueof (ticket date); // cannot be forcibly converted

In this case, the database field is of the timestamp (6) type.

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

Appendix: valueof () methods of several date classes

1. java. SQL. Date. valueof (Java. Lang. String)

Public static date valueof (string S)
Converts a string in JDBC date escape format to a date value.

Parameters:
S-A String object representing a date in the format "yyyy-mm-dd"
Returns:
A java. SQL. date object representing the given date
Throws:
Illegalargumentexception-if the date given is not in the JDBC date escape format (yyyy-mm-dd)

2. java. SQL. Time. valueof (Java. Lang. String)

Public static time valueof (string S)
Converts a string in JDBC time escape format to a time value.

Parameters:
S-time in format "HH: mm: SS"
Returns:
A corresponding time object

3. java. SQL. timestamp. valueof (Java. Lang. String)

Public static timestamp valueof (string S)
Converts a String object in JDBC timestamp escape format to a timestamp value.

parameters:
S-timestamp in format yyyy-mm-dd hh: mm: Ss. fffffffff
Returns:
corresponding timestamp value
throws:
illegalargumentexception-if the given argument does not have the format yyyy-mm-dd hh: mm: SS. fffffffff

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.