cn.lkk.web.util.SqlUtil Maven / Gradle / Ivy
The newest version!
package cn.lkk.web.util;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import cn.lkk.web.exception.DataModelException;
/**
* @author wangfujun
* @date 2018/12/18
*/
public class SqlUtil {
public static final int TYPE_Integer = 1;
public static final int TYPE_Long = 2;
public static final int TYPE_Float = 3;
public static final int TYPE_Double = 4;
public static final int TYPE_Boolean = 5;
public static final int TYPE_String = 6;
public static final int TYPE_Date = 7;
public static final int TYPE_Timestamp = 8;
private final static SimpleDateFormat sdf8 = new SimpleDateFormat("yyyyMMdd");
private final static SimpleDateFormat sdf10 = new SimpleDateFormat("yyyy-MM-dd");
private final static SimpleDateFormat sdf19 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static Serializable convertStandarValue(Object val, int type) {
Serializable newValue = null;
if (val == null) {
return null;
}
String value = val.toString();
if (type == TYPE_Integer) {
newValue = new Integer(value);
} else if (type == TYPE_Long) {
newValue = new Long(value);
} else if (type == TYPE_Double) {
newValue = new Double(value);
} else if (type == TYPE_Float) {
newValue = new Float(value);
} else if (type == TYPE_String) {
newValue = value;
} else if (type == TYPE_Date) {
try {
if (value.length() == 8) {
newValue = sdf8.parse(value);
} else if (value.length() == 10) {
newValue = sdf10.parse(value);
} else if (value.length() == 19) {
newValue = sdf19.parse(value);
}
} catch (ParseException e) {
throw new DataModelException("日期转换失败:[" + value + "]");
}
} else if (type == TYPE_Timestamp) {
try {
if (value.length() == 8) {
newValue = sdf8.parse(value);
} else if (value.length() == 10) {
newValue = sdf10.parse(value);
} else if (value.length() == 19) {
newValue = sdf19.parse(value);
}
} catch (ParseException e) {
throw new DataModelException("日期转换失败:[" + value + "]");
}
}
return newValue;
}
}