首先写了一个公用的转换帮助类 package com.dzf.test; import java.math.BigDecimal; import java.sql.Date; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; public class ConvertHelper ... { public static final StringDecimalFormat = " ###,###,###,##0.00 " ; public static final StringDateFormat = " yyyy-MM-dd " ; /***/ /** *把字符串转换为整数。 * * @param str *待转换的字符串 * @return 整数 * @throws Exception *如果字符串格式不对 */ public static int StringToInt(Stringstr) ... { if (str == null || str.equals( "" )) ... { return 0 ; } else ... { return Integer.parseInt(str); } } /***/ /** *把字符串转换为长整形。 * * @param str *待转换的字符串 * @return 整数 * @throws Exception *如果字符串格式不对 */ public static long StringToLong(Stringstr) ... { if (str == null || str.equals( "" )) return 0 ; return StringToNumber(str).longValue(); } /***/ /** *把字符串转换为DOUBLE。 * * @param str *待转换的字符串 * @return 整数 * @throws Exception *如果字符串格式不对 */ public static double StringToDouble(Stringstr) ... { if (str == null || str.equals( "" )) return 0 ; return StringToNumber(str).doubleValue(); } /***/ /** *把字符串转换为Decimal。 * * @param str *待转换的字符串 * @return 整数 * @throws Exception *如果字符串格式不对 */ public static BigDecimalStringToDecimal(Stringstr) ... { return new BigDecimal(StringToNumber(str).doubleValue()); } /***/ /** *把字符串转换为NUMBER。 * * @param str *待转换的字符串 * @return 整数 * @throws Exception *如果字符串格式不对 */ public static NumberStringToNumber(Stringstr) ... { if (str == null ) return null ; if (str.equals( "" )) str = " 0 " ; Numbernumber; DecimalFormatdf = new DecimalFormat(DecimalFormat); try ... { number = df.parse(str); return new BigDecimal(number.toString()); } catch (ParseExceptione) ... { e.printStackTrace(); } return null ; } /***/ /** *把BigDecimal类型转换为字符串 * * @param number * @return */ public static StringDecimalToString(BigDecimalnumber) ... { if (number == null ) ... { return " 0.00 " ; } DecimalFormatdf = new DecimalFormat(DecimalFormat); return df.format(number); } /***/ /** *把Number类型转换为字符串 * * @param number * @return */ public static StringDecimalToString(Numbernumber) ... { if (number == null ) ... { return " 0.00 " ; } DecimalFormatdf = new DecimalFormat(DecimalFormat); return df.format(number); } /***/ /** *把double类型转换为字符串 * * @param number * @return */ public static StringDecimalToString( double number) ... { DecimalFormatdf = new DecimalFormat(DecimalFormat); return df.format(number); } /***/ /** *把long类型转换为字符串 * * @param number * @return */ public static StringDecimalToString( long number) ... { DecimalFormatdf = new DecimalFormat(DecimalFormat); return df.format(number); } /***/ /** *把java.util.Date类型转换为字符串 * * @param date * @return */ public static StringDateToString(java.util.Datedate) ... { if (date == null ) return "" ; SimpleDateFormatsdf = new SimpleDateFormat(DateFormat); return sdf.format(date); } /***/ /** *把字符串转换为Date类型 * * @param str *时间类型如"2007-10-28" * @return * @throws Exception */ public static DateStringToDate(Stringstr) ... { if (str == null || str.equals( "" )) return null ; SimpleDateFormatsdf = new SimpleDateFormat(DateFormat); Datedate; try ... { date = new Date(sdf.parse(str).getTime()); return date; } catch (ParseExceptione) ... { e.printStackTrace(); } return null ; } /***/ /** *把字符串转换为Date类型 * * @param str *时间类型如"2007-10-28" * @param format *时间格式如"yyyy-MM-dd" * @return * @throws Exception */ public static DateStringToDate(Stringstr,Stringformat) ... { if (str == null || str.equals( "" )) return null ; SimpleDateFormatsdf = new SimpleDateFormat(format); Datedate; try ... { date = new Date(sdf.parse(str).getTime()); return date; } catch (ParseExceptione) ... { e.printStackTrace(); } return null ; } /***/ /** *将OBJECT转换为STRING * * @param obj * @return 如果OBJ为NULL,则返回null */ public static StringObjToString(Objectobj) ... { return ObjToString(obj, null ); } /***/ /** *将OBJECT转换为STRING * * @param obj * @param retStr *为空时的返回值 * @return