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