The previous article introduced the use of hibernate to achieve a simple data insertion, in this process, we need to pay attention to one place is the time format problem. Before doing SSH online mall, DRP also encountered similar problems, the following time format conversion to do a small summary.
when inserting time format data, the new date () method used directly in the video instance gets the current time. However, in the demo run process, the prompt time format has problems, the time format to print out, found that the format used are as follows:
Tue 21:59:41 CST 2016
In the online store section, the workaround is to change all datetime type data to a string type, and then convert the time from the new date () to a string. And this time also encountered the problem, do not want to modify the data format, after many tests, find the following methods:
method One: Using the following statement conversion, the problem is to convert util.date to the sql.date type.
New Java.sql.Date (New Java.util.Date (). GetTime ());
Method Two:
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); The time format in the user entity is util.date format user.setcreatetime (Formatter.parse (Formatter.format (New Date ())));
Method Two also has some problems, it is to convert the data two times, first converted to a time string, and then converted to a time type. We can see that this process is: Time format--time string format--time format.
Legacy issues:
It is easy to see that method two is a repetitive conversion process, and given such a conversion, a simple test has been done to find some problems. The test is as follows:
Package Com.tgb.hibernate;import Java.text.parseexception;import Java.text.simpledateformat;import java.util.Date; public class Test {/** * time format testing * @param args * @throws parseexception */public static void Main (string[] args) throws parseexception {SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); System.out.println (Formatter.parse ((Formatter.format (New Date ())));//two-time conversion System.out.println ((Formatter.parse ( Formatter.format (new Date ()))). GetClass ());//two post-conversion type SYSTEM.OUT.PRINTLN (new date ());//Direct use of SYSTEM.OUT.PRINTLN (( New Date ()). GetClass ());//new date () type}}
The test results are as follows:
Tue 21:59:41 CST 2016class java.util.DateTue 21:59:41 CST 2016class java.util.Date
We will find that before and after the conversion, their time format and type are the same, but the pre-conversion cannot be written directly to the database, and the converted data can be written. So far, there is no understanding of the specific differences between the two, there is known to give guidance ah.
Summary:
Time format problem can be said that we often have to meet the problem, when a problem often encountered, we can not only be limited to solve it, but also to explore the underlying causes of the problem and the principle of problem-solving methods. In the repeated study, constantly improve their understanding of the degree, but also exercise their ability to explore.
"Hibernate"--time format processing