java 对象转二进制_类型处理工具类TypeUtils实现对象二进制间转换、对象常见基本数据类型间互转换等代码示例...

一、前言

通过定义类型TypeUtils工具类(依赖fastjson包),实现常用数据类型和对象间的相互转换 - 对象转换日期castToSqlDate、对象转时间戳castToTimestamp、对象转大精度数据类型castToBigDecimal、对象转字符串castToString等。

二、代码示例import java.math.BigDecimal;@b@import java.math.BigInteger;@b@import java.text.ParseException;@b@import java.text.SimpleDateFormat;@b@import java.util.Calendar;@b@import java.util.Date;@b@@b@import com.alibaba.fastjson.JSON;@b@import com.alibaba.fastjson.JSONException;@b@import com.alibaba.fastjson.parser.JSONScanner;@b@import com.alibaba.fastjson.util.Base64;@b@@b@public class TypeUtils {@b@@b@public static final String castToString(Object value) {@b@        if (value == null) {@b@            return null;@b@        }@b@@b@        return value.toString();@b@    }@b@@b@    public static final Byte castToByte(Object value) {@b@        if (value == null) {@b@            return null;@b@        }@b@@b@        if (value instanceof Number) {@b@            return ((Number) value).byteValue();@b@        }@b@@b@        if (value instanceof String) {@b@            String strVal = (String) value;@b@            if (strVal.length() == 0) {@b@                return null;@b@            }@b@            return Byte.parseByte(strVal);@b@        }@b@@b@        throw new JSONException("can not cast to byte, value : " + value);@b@    }@b@@b@    public static final Character castToChar(Object value) {@b@        if (value == null) {@b@            return null;@b@        }@b@@b@        if (value instanceof Character) {@b@            return (Character) value;@b@        }@b@@b@        if (value instanceof String) {@b@            String strVal = (String) value;@b@@b@            if (strVal.length() == 0) {@b@                return null;@b@            }@b@@b@            if (strVal.length() != 1) {@b@                throw new JSONException("can not cast to byte, value : " + value);@b@            }@b@@b@            return strVal.charAt(0);@b@        }@b@@b@        throw new JSONException("can not cast to byte, value : " + value);@b@    }@b@@b@    public static final Short castToShort(Object value) {@b@        if (value == null) {@b@            return null;@b@        }@b@@b@        if (value instanceof Number) {@b@            return ((Number) value).shortValue();@b@        }@b@@b@        if (value instanceof String) {@b@            String strVal = (String) value;@b@            if (strVal.length() == 0) {@b@                return null;@b@            }@b@            return Short.parseShort(strVal);@b@        }@b@@b@        throw new JSONException("can not cast to short, value : " + value);@b@    }@b@@b@    public static final BigDecimal castToBigDecimal(Object value) {@b@        if (value == null) {@b@            return null;@b@        }@b@@b@        if (value instanceof BigDecimal) {@b@            return (BigDecimal) value;@b@        }@b@@b@        if (value instanceof BigInteger) {@b@            return new BigDecimal((BigInteger) value);@b@        }@b@@b@        String strVal = value.toString();@b@        if (strVal.length() == 0) {@b@            return null;@b@        }@b@@b@        return new BigDecimal(strVal);@b@    }@b@@b@    public static final BigInteger castToBigInteger(Object value) {@b@        if (value == null) {@b@            return null;@b@        }@b@@b@        if (value instanceof BigInteger) {@b@            return (BigInteger) value;@b@        }@b@@b@        if (value instanceof Float || value instanceof Double) {@b@            return BigInteger.valueOf(((Number) value).longValue());@b@        }@b@@b@        String strVal = value.toString();@b@        if (strVal.length() == 0) {@b@            return null;@b@        }@b@@b@        return new BigInteger(strVal);@b@    }@b@@b@    public static final Float castToFloat(Object value) {@b@        if (value == null) {@b@            return null;@b@        }@b@@b@        if (value instanceof Number) {@b@            return ((Number) value).floatValue();@b@        }@b@@b@        if (value instanceof String) {@b@            String strVal = value.toString();@b@            if (strVal.length() == 0) {@b@                return null;@b@            }@b@@b@            return Float.parseFloat(strVal);@b@        }@b@@b@        throw new JSONException("can not cast to float, value : " + value);@b@    }@b@@b@    public static final Double castToDouble(Object value) {@b@        if (value == null) {@b@            return null;@b@        }@b@@b@        if (value instanceof Number) {@b@            return ((Number) value).doubleValue();@b@        }@b@@b@        if (value instanceof String) {@b@            String strVal = value.toString();@b@            if (strVal.length() == 0) {@b@                return null;@b@            }@b@            return Double.parseDouble(strVal);@b@        }@b@@b@        throw new JSONException("can not cast to double, value : " + value);@b@    }@b@@b@    public static final Date castToDate(Object value) {@b@        if (value == null) {@b@            return null;@b@        }@b@@b@        if (value instanceof Calendar) {@b@            return ((Calendar) value).getTime();@b@        }@b@@b@        if (value instanceof Date) {@b@            return (Date) value;@b@        }@b@@b@        long longValue = -1;@b@@b@        if (value instanceof Number) {@b@            longValue = ((Number) value).longValue();@b@        }@b@@b@        if (value instanceof String) {@b@            String strVal = (String) value;@b@@b@            if (strVal.indexOf('-') != -1) {@b@                String format;@b@                if (strVal.length() == JSON.DEFFAULT_DATE_FORMAT.length()) {@b@                    format = JSON.DEFFAULT_DATE_FORMAT;@b@                } else if (strVal.length() == 10) {@b@                    format = "yyyy-MM-dd";@b@                } else if (strVal.length() == "yyyy-MM-dd HH:mm:ss".length()) {@b@                    format = "yyyy-MM-dd HH:mm:ss";@b@                } else {@b@                    format = "yyyy-MM-dd HH:mm:ss.SSS";@b@                }@b@@b@                SimpleDateFormat dateFormat = new SimpleDateFormat(format);@b@                try {@b@                    return (Date) dateFormat.parse(strVal);@b@                } catch (ParseException e) {@b@                    throw new JSONException("can not cast to Date, value : " + strVal);@b@                }@b@            }@b@@b@            if (strVal.length() == 0) {@b@                return null;@b@            }@b@@b@            longValue = Long.parseLong(strVal);@b@        }@b@@b@        if (longValue 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值