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)
[Java]
- <span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >package Com.hudong.util.orther;
- Importjava.sql.Timestamp;
- Importjava.text.ParseException;
- Importjava.text.SimpleDateFormat;
- Import Java.util.Date;
- public class Transformdate {
- /**
- * Use the current time only as a condition for the MySQL timestamp field by date (time 0)
- * Final return Time type Java.sql.Date
- */
- Public Voidtransformcurdate () {
- SimpleDateFormat format = new SimpleDateFormat ("yyyy-mm");
- Java.sql.Date Timepara = null;
- try {
- Timepara = new Java.sql.Date (new Date (). 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
- */
- Public Voidtransformcuryearmon () {
- SimpleDateFormat format = new SimpleDateFormat ("yyyy-mm");
- String time = Format.format (new Date ()). 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 ();
- }
- }
- /**
- * Convert the current time of Java to timestamp as a condition of the MySQL timestamp field
- * Final return Time type Java.sql.Timestamp
- */
- public static void TestData () {
- try {
- SimpleDateFormat SDF = new SimpleDateFormat ("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 is 0)
- * Final return Time type Java.util.Date
- */
- public static void Datatest () {
- try {
- SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");
- String time = Format.format (new Date ());
- Date date = Format.parse (Time.concat ("00:00:00"));
- SYSTEM.OUT.PRINTLN (date);
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- public static void Main (String[]args) {
- TestData ();
- }
- }
- </span>
(RPM) Java Four ways to insert the current time into MySQL and several ways to format Java time date (case description)