一、前言
通过定义类型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) {@b@ throw new JSONException("can not cast to Date, value : " + value);@b@ }@b@@b@ return new Date(longValue);@b@ }@b@@b@ public static final java.sql.Date castToSqlDate(Object value) {@b@ if (value == null) {@b@ return null;@b@ }@b@@b@ if (value instanceof Calendar) {@b@ return new java.sql.Date(((Calendar) value).getTimeInMillis());@b@ }@b@@b@ if (value instanceof java.sql.Date) {@b@ return (java.sql.Date) value;@b@ }@b@@b@ if (value instanceof java.util.Date) {@b@ return new java.sql.Date(((java.util.Date) value).getTime());@b@ }@b@@b@ long longValue = 0;@b@@b@ if (value instanceof Number) {@b@ longValue = ((Number) value).longValue();@b@ }@b@@b@ if (value instanceof String) {@b@ String strVal = (String) value;@b@ if (strVal.length() == 0) {@b@ return null;@b@ }@b@@b@ longValue = Long.parseLong(strVal);@b@ }@b@@b@ if (longValue <= 0) {@b@ throw new JSONException("can not cast to Date, value : " + value);@b@ }@b@@b@ return new java.sql.Date(longValue);@b@ }@b@@b@ public static final java.sql.Timestamp castToTimestamp(Object value) {@b@ if (value == null) {@b@ return null;@b@ }@b@@b@ if (value instanceof Calendar) {@b@ return new java.sql.Timestamp(((Calendar) value).getTimeInMillis());@b@ }@b@@b@ if (value instanceof java.sql.Timestamp) {@b@ return (java.sql.Timestamp) value;@b@ }@b@@b@ if (value instanceof java.util.Date) {@b@ return new java.sql.Timestamp(((java.util.Date) value).getTime());@b@ }@b@@b@ long longValue = 0;@b@@b@ if (value instanceof Number) {@b@ longValue = ((Number) value).longValue();@b@ }@b@@b@ if (value instanceof String) {@b@ String strVal = (String) value;@b@ if (strVal.length() == 0) {@b@ return null;@b@ }@b@@b@ longValue = Long.parseLong(strVal);@b@ }@b@@b@ if (longValue <= 0) {@b@ throw new JSONException("can not cast to Date, value : " + value);@b@ }@b@@b@ return new java.sql.Timestamp(longValue);@b@ }@b@@b@ public static final Long castToLong(Object value) {@b@ if (value == null) {@b@ return null;@b@ }@b@@b@ if (value instanceof Number) {@b@ return ((Number) value).longValue();@b@ }@b@@b@ if (value instanceof String) {@b@ String strVal = (String) value;@b@ if (strVal.length() == 0) {@b@ return null;@b@ }@b@@b@ try {@b@ return Long.parseLong(strVal);@b@ } catch (NumberFormatException ex) {@b@ //@b@ }@b@@b@ JSONScanner dateParser = new JSONScanner(strVal);@b@ Calendar calendar = null;@b@ if (dateParser.scanISO8601DateIfMatch(false)) {@b@ calendar = dateParser.getCalendar();@b@ }@b@ dateParser.close();@b@@b@ if (calendar != null) {@b@ return calendar.getTimeInMillis();@b@ }@b@ }@b@@b@ throw new JSONException("can not cast to long, value : " + value);@b@ }@b@@b@ public static final Integer castToInt(Object value) {@b@ if (value == null) {@b@ return null;@b@ }@b@@b@ if (value instanceof Integer) {@b@ return (Integer) value;@b@ }@b@@b@ if (value instanceof Number) {@b@ return ((Number) value).intValue();@b@ }@b@@b@ if (value instanceof String) {@b@ String strVal = (String) value;@b@ if (strVal.length() == 0) {@b@ return null;@b@ }@b@@b@ return Integer.parseInt(strVal);@b@ }@b@@b@ throw new JSONException("can not cast to int, value : " + value);@b@ }@b@@b@ public static final byte[] castToBytes(Object value) {@b@ if (value instanceof byte[]) {@b@ return (byte[]) value;@b@ }@b@@b@ if (value instanceof String) {@b@ return Base64.decodeFast((String) value);@b@ }@b@ throw new JSONException("can not cast to int, value : " + value);@b@ }@b@@b@ public static final Boolean castToBoolean(Object value) {@b@ if (value == null) {@b@ return null;@b@ }@b@@b@ if (value instanceof Boolean) {@b@ return (Boolean) value;@b@ }@b@@b@ if (value instanceof Number) {@b@ return ((Number) value).intValue() == 1;@b@ }@b@@b@ if (value instanceof String) {@b@ String str = (String) value;@b@ if (str.length() == 0) {@b@ return null;@b@ }@b@@b@ if ("true".equals(str)) {@b@ return Boolean.TRUE;@b@ }@b@ if ("false".equals(str)) {@b@ return Boolean.FALSE;@b@ }@b@@b@ if ("1".equals(str)) {@b@ return Boolean.TRUE;@b@ }@b@ return Boolean.FALSE;@b@ }@b@@b@ throw new JSONException("can not cast to int, value : " + value);@b@ }@b@ @b@ public static final int castToInt(String value, int def) {@b@ if (value == null) return def;@b@ try {@b@ return Integer.parseInt(value);@b@ } catch (Exception e) {@b@ return def;@b@ }@b@ }@b@}