Wednesday, June 18, 2008

Convert Date object to Mysql Date Format


public static String toMysqlDateStr(Date date){
if (date==null) return "NULL";
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sqlValueWithQuotas(sdf.format(date));
}

public static String sqlValueWithQuotas(Object obj){
if ( obj == null ) return "NULL";

String str = obj.toString();
str.replaceAll("'", "\\'");
str = '\''+str+'\'';

return str;

}

0 comments: