zoukankan      html  css  js  c++  java
  • java Object转换成指定的类型

    java Object转换成指定的类型

     /**
         * Object转成指定的类型
         * @param obj
         * @param type
         * @param <T>
         * @return
         */
        public static<T> T convert(Object obj, Class<T> type) {
            if (obj != null && StringUtils.isNotBlank(obj.toString())) {
                if (type.equals(Integer.class)||type.equals(int.class)) {
                    return (T)new Integer(obj.toString());
                } else if (type.equals(Long.class)||type.equals(long.class)) {
                    return (T)new Long(obj.toString());
                } else if (type.equals(Boolean.class)||type.equals(boolean.class)) {
                    return (T) new Boolean(obj.toString());
                } else if (type.equals(Short.class)||type.equals(short.class)) {
                    return (T) new Short(obj.toString());
                } else if (type.equals(Float.class)||type.equals(float.class)) {
                    return (T) new Float(obj.toString());
                } else if (type.equals(Double.class)||type.equals(double.class)) {
                    return (T) new Double(obj.toString());
                } else if (type.equals(Byte.class)||type.equals(byte.class)) {
                    return (T) new Byte(obj.toString());
                } else if (type.equals(Character.class)||type.equals(char.class)) {
                    return (T)new Character(obj.toString().charAt(0));
                } else if (type.equals(String.class)) {
                    return (T) obj;
                } else if (type.equals(BigDecimal.class)) {
                    return (T) new BigDecimal(obj.toString());
                } else if (type.equals(LocalDateTime.class)) {
                    //DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    return (T) LocalDateTime.parse(obj.toString());
                } else if (type.equals(Date.class)) {
                    try
                    {
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
                        return (T) formatter.parse(obj.toString());
                    }
                    catch (ParseException e)
                    {
                       throw new RuntimeException(e.getMessage());
                    }
    
                }else{
                    return null;
                }
            } else {
                if (type.equals(int.class)) {
                    return (T)new Integer(0);
                } else if (type.equals(long.class)) {
                    return (T)new Long(0L);
                } else if (type.equals(boolean.class)) {
                    return (T)new Boolean(false);
                } else if (type.equals(short.class)) {
                    return (T)new Short("0");
                } else if (type.equals(float.class)) {
                    return (T) new Float(0.0);
                } else if (type.equals(double.class)) {
                    return (T) new Double(0.0);
                } else if (type.equals(byte.class)) {
                    return (T) new Byte("0");
                } else if (type.equals(char.class)) {
                    return (T) new Character('u0000');
                }else {
                    return null;
                }
            }
        }
  • 相关阅读:
    各种视角了解数组Array以及案例展示
    js基础面试高频面点2:Javascript中undefined和not defined有什么区别,和null又有什么联系?
    一些好用的Linux命令工具
    Linux实用命令工具-dtrx根据需要自动解压
    J2EE--Hibernate基础笔记
    mysql用户与权限管理笔记
    找出字符串中出现频率最少的字符,并将其去除
    Linux常用命令--文件(夹)查找之find命令
    J2EE--Struts2基础开发笔记
    Java多线程基础知识总结笔记
  • 原文地址:https://www.cnblogs.com/puke/p/7228460.html
Copyright © 2011-2022 走看看