Java four ways to insert the current time into MySQL and several ways to format Java time date (case description); Part of the Data Reference network resources
1. Four ways Java inserts the current time into MySQL
First: Convert the time of the java.util.Date type to the Java.sql.Date type time recognized by the MySQL database
Note: Java.util.Date is the parent class of Java.sql.Date
Date time= New Java.sql.Date (Newjava.util.Date (). GetTime ());
The second kind: Java uses the PreparedStatement to Setdate, assigns the date question mark in the form question mark the value
Pstmt.settimestamp (8, Newtimestamp (System.currenttimemillis ()));
Pstmt.setdate (1, New Java.sql.Date (Newdate (). GetTime ()));
Third: How to use Hibernate to provide database operations
In fact, it is easy to insert a time field into the MySQL database, as long as the java.util.Date type is set to the Pojo class object in Hibernate, Pojo.set (New Java.util.Date ()) is available.
IV: Using the valueof method of timestamp
The following appendices are found on the Internet:
Mysql vs. Java time types
The time type of MySQL has the corresponding time type in Java
Date Java.sql.Date
Datetime Java.sql.Timestamp
Timestamp Java.sql.Timestamp
Time Java.sql.Time
Year Java.sql.Date
This is done in the following way:
Date date = new Date ();//Gain system time.
String nowtime = new SimpleDateFormat ("Yyyy-mm-ddhh:mm:ss"). Format (date),//Convert the time format to a format that meets the timestamp requirements.
Timestamp goodsc_date =timestamp.valueof (nowtime);//Convert time
2. Several ways to format Java time and date (case description)
Importjava.sql.timestamp;importjava.text.parseexception;importjava.text.simpledateformat;import Java.util.Date; Public classTransformdate {/** * Direct the current time only by date (time 0) as the condition of the MySQL timestamp field * Final return time type Java.sql.Date*/ Publicvoidtransformcurdate () {SimpleDateFormat format=NewSimpleDateFormat ("yyyy-mm"); Java.sql.Date Timepara=NULL; Try{Timepara=NewJava.sql.Date (NewDate (). GetTime ()); System. out. println (Timepara); } Catch(Exception e) {e.printstacktrace (); } } /** * Convert the current time of Java to the specified format (yyyy-mm-0100:00:00 ") as a condition of the MySQL timestamp field * Final return time type Java.sql.Date*/ PublicVoidtransformcuryearmon () {SimpleDateFormat format=NewSimpleDateFormat ("yyyy-mm"); String Time= Format.format (NewDate ()). Concat ("-0100:00:00"); Java.sql.Date Timepara=NULL; Try{Timepara=Newjava.sql.Date (Format.parse (Time). GetTime ()); System. out. println (Timepara); } Catch(ParseException e) {e.printstacktrace (); } } /** * Turn the current time of Java into Timestamp as the condition of the MySQL timestamp field * Final return time type Java.sql.Timestamp*/ Public Static voidTestData () {Try{SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-ddhh:mm:ss"); Timestamp Date= Java.sql.Timestamp.valueOf ("2012-12-1201:12:11"); System. out. println (date); } Catch(Exception e) {e.printstacktrace (); } } /** * Processing current time only by date (time 0) * Final return time type Java.util.Date*/ Public Static voiddatatest () {Try{SimpleDateFormat format=NewSimpleDateFormat ("YYYY-MM-DD"); String Time= Format.format (NewDate ()); Date Date= Format.parse (Time.concat ("00:00:00")); System. out. println (date); } Catch(Exception e) {e.printstacktrace (); } } Public Static voidMain (String[]args) {testData (); }}
..
Java four ways to insert the current time into MySQL and several ways to format Java time date (case description)