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;



Anonymous said…
Thank you, this is really what I need for this moment :P

Popular posts from this blog

Get Remote IP Address In Axis SOAP Server

Connect via SSH without a password from MacOS to Linux with an SSHKEY