How Java accesses MySQL datetime types

Source: Internet
Author: User

  

1 in Java only the date type, so that the data stored in the MySQL problem, the foreground submitted data, such as 2018-03-20 17:30:59, background with date accepted, because date only accurate to day, so the default receive time is 2016-10-10 00:00:00, after saving to the MySQL database, re-extract the data when you will find that the data for 2018-03-20 00:00:00.0, for no reason in the back of a. 0.

2 workaround

① Extracting data

If you want to get the YYYY-MM-DD HH:mm:ss date format from MySQL, you must first use the Rs.gettimestamp ("Insert_dt") method, where "Insert_dt" is the database Time field and the type is datetime And then format the class by SimpleDateFormat time, converting the time taken out to a string type

First, compare RS to get different date and time format methods

    System.out.println (Rs.getdate ("Insert_dt"));    System.out.println (Rs.gettime ("Insert_dt"));    System.out.println (Rs.gettimestamp ("Insert_dt"));

Output Result:

2018-03-1922:03:212018-03-19 22:03:21.0

You can see that the date format obtained through Gettimestamp has a last-digit number (2018-03-19 22:03:21.0), which requires the conversion of Rs.gettimestamp ("Insert_dt") to String type

New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (Rs.gettimestamp ("Insert_dt"));
System.out.println (TimeStamp);

Output Result:

2018-03-19 21:51:57

② Storing data

            New Date ();           New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");         // simulate a date format from the front end or you can        convert to a string String datestr = Format.format (date);                    String SQL= "INSERT into T_pay_info (TITLE,CONTENT,PRICE,TYPE_ID,CONTACT,PHONE,INSERT_DT)"                    + "values" (?,?, ?, ?, ?, ?, ?);" ;         = {"Junior English tutoring", "good at junior high school English tutoring, good communication with students, steady improvement of students ' performance", "3", "Latinyrain", "15185077780",datestr};        Dbutil.update (sql, params);

How Java accesses MySQL datetime types

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.