Several ways of Java inserting current time into MySQL and several ways of java time date format: (refer to network resources for information)
- Several ways of java inserting current time into MySQL
The first is to convert the time of type java.util.Date to the time of type java.sql.Date recognized by mysql database
Note: java.util.Date is the parent class of java.sql.Date
Upward Transformation: I have defined a subclass Cat, which inherits the Animal class, so the latter is the former is the parent class.
Instantiate a Cat object through Cat c = new Cat(); but when I define it like this: Animal a = new Cat();
It means that I have defined a reference of the Animal type, pointing to the object of the new Cat type.
Because Cat inherits from its parent, Animal, a reference to an Animal type can point to an object of Cat type.
Date time= new java.sql.Date(newjava.util.Date().getTime());
Second, java uses PreparedStatement to set date, and uses question mark to assign value to date question mark
pstmt.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
pstmt.setDate(1, new java.sql.Date(newDate().getTime()));
Third: use the database operation method provided by hibernate
As long as it is set to java.util.Date type, take the Pojo class object of Hibernate as an example, pojo.set(new java.util.Date()); is available.
Time types of Mysql and java
The time type of MySql has the corresponding time type in ava
date java.sql.Date
Datetime java.sql.Timestamp
Timestamp java.sql.Timestamp
Time java.sql.Time
Year java.sql.Date
So it can be realized in the following ways:
Date date = new Date();//Get system time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
String nowTime = sdf.format(date);//Convert the time format to a format that meets the Timestamp requirements
Timestamp dates =Timestamp.valueOf(nowTime);//Change the time
- Several methods of java time date format
importjava.sql.Timestamp;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
import java.util.Date;
public class TransformDate {
/**
* Directly take the current time as the condition of mysql timestamp field only by date (time is 0)
* The final return time type is java.sql.Date
*/
public void transformCurDate(){
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 the condition of mysql timestamp field
* The final return time type is java.sql.Date
*/
public void transformCurYearMon(){
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 java's current time to Timestamp as the condition of mysql Timestamp field
* The final return time type is 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();
}
}
/**
* Process current time by date only (time 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();
}
}
}