Insert time in the format of "yyyy-mm-dd hh: mm: SS" to MySQL

Source: Internet
Author: User
Tags time and seconds
A table contains a field of the datetime type, and you want to insert a time to it in the format of "yyyy-mm-dd hh: mm: SS ";
First, execute ps. setdate (2, date) in SQL )... After successful insertion, the time and seconds of the background database are always 00:00:00.
I have already formatted the statement before executing the SQL statement.
 
Simpledateformat SFD = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); string r1_tdate = SFD. format (date); Java. SQL. date date2 = Java. SQL. date. valueof (cfinishdate); PS. setdate (7, date2 );

The above method directly generates a bug:

 
Java. Lang. numberformatexception: for input string: "18 02:18:15"

Don't understand, just ask du Niang, and find that the vast majority of us are called first format first, then insert.
In addition, I inserted the current time into MySQL and tried it once as someone else said:

 
PS. setdate (2, new java. SQL. Date (long) New java. util. Date (). gettime ()));

As a result, there is no record in MySQL data, that is, time, minute, and second. 00:00:00 is displayed.

In fact, I always thought that in MySQL, data in the format of date, datetime, and other time types should be used to execute SQL insert in Java, and the PS. setdate method should be used,
You can use setstring to insert data directly. The format must be in advance. After experiment, it is really convenient to insert with setstring.

Besides, if it is still a field of the datetime type, it should be read from the database in the format of "yyyy-mm-dd hh: mm: SS. You can only read "yyyy-mm-dd.

Therefore, the best way is to change the field type to varchar to store the time. This greatly saves the trouble of format conversion.

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.