I haven’t used Mysql for a long time, and I encountered a lot of trouble with small time issues, haha
So to summarize:
The first type:
Date time= new java.sql.Date(new java.util.Date().getTime());
Second type:
java uses PreparedStatement to setDate and assigns value to the date question mark in the form of question mark
pstmt.setTimestamp(8, new Timestamp(System.currentTimeMillis()));
pstmt.setDate(1, new java.sql.Date(date1.getTime()));
pstmt.setDate(2, new java.sql.Date(date2.getTime()));
third:
In fact, it is very easy to insert a time field into the mysql database. As long as it is set to the java.util.Date type, taking Hibernate's Pojo class object as an example, pojo.set(new java.util.Date()); will be available. .
The following appendix contains relevant information found online:
Mysql and 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
So it is achieved in the following ways:
Date date = new Date();//Get the system time.
String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);//Convert the time format into a format that meets Timestamp requirements.
Timestamp goodsC_date = Timestamp.valueOf(nowTime);//Convert time
java.util.Date is the parent class of java.sql.Date
-