实用代码块记录3

来源:互联网 发布:西安网络家装设计公司 编辑:程序博客网 时间:2024/05/21 10:34

1.将对象obj安全转换int数据

    public static int convertInt(Object obj, int defaultValue) {        if (obj != null) {            String value = obj.toString();            if (!CheckUtils.isStrEmpty(value) && CheckUtils.isNumber(value)) {                return Integer.valueOf(value);            }        }        return defaultValue;    }

2.将对象obj安全转换double数据

public static double convertDouble(Object obj, double defaultValue) {        if (obj != null) {            String value = obj.toString();            if (!CheckUtils.isStrEmpty(value)) {                try {                    return Double.valueOf(value);                } catch (Exception e) {                    return defaultValue;                }            }        }        return defaultValue;    }

3.将对象obj安全转换float数据

 public static float convertFloat(Object obj, float defaultValue) {        if (obj != null) {            String value = obj.toString();            if (!CheckUtils.isStrEmpty(value)) {                try {                    return Float.valueOf(value);                } catch (Exception e) {                    return defaultValue;                }            }        }        return defaultValue;    }

4.将对象obj安全转换short数据

public static short convertShort(Object obj, short defaultValue) {        if (obj != null) {            String value = obj.toString();            if (!CheckUtils.isStrEmpty(value) && CheckUtils.isNumber(value)) {                return Short.valueOf(value);            }        }        return defaultValue;    }

5.将对象obj安全转换long数据

 public static long convertLong(Object obj, long defaultValue) {        if (obj != null) {            String value = obj.toString();            if (!CheckUtils.isStrEmpty(value) && CheckUtils.isNumber(value)) {                return Long.valueOf(value);            }        }        return defaultValue;    }

6.将对象obj安全转换boolean数据

public static boolean convertBoolean(Object obj, boolean defaultValue) {        if (obj != null) {            String value = obj.toString();            if (!CheckUtils.isStrEmpty(value)) {                try {                    return Boolean.valueOf(value);                } catch (Exception e) {                    return defaultValue;                }            }        }        return defaultValue;    }
0 0
原创粉丝点击