cq.database.util.ThisUtil Maven / Gradle / Ivy
The newest version!
package cq.database.util;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
public class ThisUtil {
public static String date2Str(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
public static T objCast(Object obj, Class clazz) {
if (obj == null) {
return null;
}
if (Number.class.isAssignableFrom(clazz) || clazz == Boolean.class) {
try {
return clazz.getConstructor(String.class).newInstance(obj + "" == "" ? "0" : obj + "");
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
return null;
}
}
return (T) obj;
}
public static boolean isNumber(Object o) {
return Pattern.matches("^(-?\\d+)(\\.\\d+)?$", o + "");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy