Java.Utils:Object 工具类

Don’t say much, just go to the code.

package org.bood.common.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Object 工具类
 *
 * @author bood
 * @since 2020/9/25
 */
public class ObjectUtils {

    private ObjectUtils() {
    }


    public static boolean isNull(Object o) {
        return o == null ? true : false;
    }

    public static boolean equals(Object o1, Object o2) {
        if (o1.equals(o2)) {
            return true;
        }
        return false;
    }

    private static boolean check(Object o) {
        return o == null ? false : true;
    }

    public static Integer parseInt(Object o) {
        if (!check(o)) {
            return null;
        }
        if (o instanceof Integer) {
            return (Integer) o;
        }
        return Integer.parseInt(o.toString());
    }

    public static Long parseLong(Object o) {
        if (!check(o)) {
            return null;
        }
        if (o instanceof Long) {
            return (Long) o;
        }
        return Long.parseLong(o.toString());
    }

    public static Short parseShort(Object o) {
        if (!check(o)) {
            return null;
        }
        if (o instanceof Short) {
            return (Short) o;
        }
        return Short.parseShort(o.toString());
    }

    public static Float parseFloat(Object o) {
        if (!check(o)) {
            return null;
        }
        if (o instanceof Float) {
            return (Float) o;
        }
        return Float.parseFloat(o.toString());
    }

    public static Double parseDouble(Object o) {
        if (!check(o)) {
            return null;
        }
        if (o instanceof Double) {
            return (Double) o;
        }
        return Double.parseDouble(o.toString());
    }

    public static Boolean parseBoolean(Object o) {
        if (!check(o)) {
            return null;
        }
        if (o instanceof Boolean) {
            return (Boolean) o;
        }
        return Boolean.parseBoolean(o.toString());
    }

    public static Date parseDate(Object o, String dateFormat) throws Exception {
        if (!check(o)) {
            return null;
        }
        if (o instanceof Date) {
            return (Date) o;
        }
        if (o instanceof Long) {
            return new Date((Long) o);
        }
        if (o instanceof String) {
            return new SimpleDateFormat(dateFormat).parse((String) o);
        }
        throw new Exception("Sorry,I don't know how to parse this value to a date");
    }

    public static <T> void clone(T t1, T t2) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
        Method[] t_methods = t1.getClass().getDeclaredMethods();
        for (Method m : t_methods) {
            if (m.getName().startsWith("set")) {
                m.invoke(t2, new Object[]{
                        t1.getClass().getMethod(m.getName().replaceFirst("s", "g"), null).invoke(t1, null)
                });
            }
        }
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值