This transformation is often met in the project and is briefly described:
(1) Date converted to datetime
DateTime corresponds to the timestamp in the Java database and is converted as follows:
Java.util.Date Convert to timestamp
date = new Date ();
Timestamp Timestamp = new Timestamp (Date.gettime ()); 2013-01-14 22:45:36.484
(2) string converted to date
String is converted to java.util.Date
string str = "2013-01-14";
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
Date date = null; Initializes the date
try {
date = Sdf.parse (str);//mon the 00:00:00 CST 2013
} catch (ParseException e) {
E.prin Tstacktrace ();
}
(3) Date converted to string
Java.util.Date Convert to string
date = new Date ();
DateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");
String str = format.format (date); 2013-01-14
(4) string converted to Java.sql.Date
Convert to java.sql.Date The purpose is to manipulate the database
String is converted to java.sql.Date
string str = "2013-01-14";
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
Java.sql.Date sdate = null; Initializes the
try {
java.util.Date udate = sdf.parse (str);
Sdate = new Java.sql.Date (Udate.gettime ()); 2013-01-14
} catch (ParseException e) {
e.printstacktrace ();
}