//yyyy-MM-dd hh:mm:ss
public static Date dateTimeString2Date(String date_str) {
try {
Calendar cal = Calendar.getInstance();
java.sql.Timestamp timestampnow = new java.sql.Timestamp(cal.getTimeInMillis());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
java.util.Date current = formatter.parse(date_str, pos);
timestampnow = new java.sql.Timestamp(current.getTime());
return timestampnow;
}
catch (NullPointerException e) {
return null;
}
}
//格式以上面不一样yyyy-MM-dd
public static Date dateString2Date(String date_str) {
try {
Calendar cal = Calendar.getInstance();
java.sql.Timestamp timestampnow = new java.sql.Timestamp(cal.getTimeInMillis());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
java.util.Date current = formatter.parse(date_str, pos);
return current;
}
catch (NullPointerException e) {
return null;
}
}
至于插入数据库,可以用java.sql.PreparedStatement;
to_date('2008-01-16 16:00:00','YYYY-MM-DD hh24:mi:ss') 如果是直接用JDBC的话 需要这种转换