This conversion is often encountered in projects, and I will briefly explain it:
(1) Convert Date to Datetime
Datetime corresponds to Timestamp in the java database, and the conversion is as follows:
//Convert to Timestamp
Date date = new Date();
Timestamp timestamp = new Timestamp(()); //2013-01-14 22:45:36.484
(2) Convert String to Date
//Convert String to
String str = "2013-01-14";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null; //Initialize date
try {
date = (str); //Mon Jan 14 00:00:00 CST 2013
} catch (ParseException e) {
();
}
(3) Convert Date to String
//Convert to String
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String str = (date); //2013-01-14
(4) String converted to
The purpose of converting to operate on the database
//Convert String to
String str = "2013-01-14";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdate = null; //Initialization
try {
udate = (str);
sdate = new (()); //2013-01-14
} catch (ParseException e) {
();
}