public static String DateToString(Date tempDate) {
String date_str = "";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 规定日期格式
try {
date_str = formatter.format(tempDate);
} catch (Exception ex) {
date_str = "";
}
return date_str;
}
/**
* 把String转换成Date,形式为yyyy-MM-dd HH:mm:ss
*/
public static Date StringToDate(String dateStr) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 规定日期格式
long long_temp = 0;
Date tempDate;
try {
long_temp = formatter.parse(dateStr).getTime();// 先转换成long型
tempDate = new Date(long_temp); // 在转换成Date型
} catch (Exception ex) {
tempDate = null;
}
return tempDate;
}
注释 都有基本不难,项目中都会用到