FormatUtil类型格式转换
package cn.edu.hbcf.common.utils; import java.math.BigDecimal; import java.math.BigInteger; import java.util.HashMap; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * FormatUtil.class 字符串,数字之间的转换工具类。 * * @author*/ public class FormatUtil { private static final Log log = LogFactory.getLog(FormatUtil.class); /** * Integer转换为String,如果integer==null则返回null * * @param integer * @return */ public static final String convertIntegerToString(Integer integer) { if (integer == null) { return null; } return integer.toString(); } // add by sxy:把一个字符串 前面加0直到8位,如时间9080808转成09080808 public static final String convertStringTo8DigitString(String str) { if (str == null) { return null; } String result = ""; int length = (8 - str.length()); for (int i = 0; i < length; i++) { result = result + "0"; } return result + str; } /** * Short转换为String,如果parashort==null则返回null * * @param parashort * @return */ public static final String convertShortToString(Short parashort) { if (parashort == null) { return null; } return parashort.toString(); } /** * CHECKBOX 转化 * * @param parashort * @return */ public static final String checkboxToString(String str) { if (str == null) { return "0"; } else { return "1"; } } /** * BigDecimal转换为String,如果bigDecimal==null则返回null * * @param bigDecimal * @return */ public static final String convertBigDecimalToString(BigDecimal bigDecimal) { if (bigDecimal == null) { return null; } return bigDecimal.toString(); } /** * 字符串转换为BigDecimal,转换失败返回null. * * @param num * @return */ public static final BigDecimal convertStringToDecimal(String num) { BigDecimal bigDecimal = null; try { bigDecimal = new BigDecimal(num); } catch (Exception e) { log.warn("FormatUtil.convertStringToDecimal(" + num + ") failed with the errorMessage:" + e.getMessage()); } return bigDecimal; } /** * 字符串转换为Integer,转换失败返回null. * * @param aStr * @return */ public static final Integer StringToInt(String aStr) { Integer integer = 0; try { if (!isEmpty(aStr)) { integer = new Integer(aStr); } } catch (Exception e) { log.warn("FormatUtil.StringToInt(" + aStr + ") failed with the errorMessage:" + e.getMessage()); } return integer; } /** * 字符串是否为空:str==null 或者str为空串返回true;否则返回false * * @param str * @return */ public static final boolean isEmpty(String str) { return (str == null || str.trim().length() == 0) ? true : false; } /** * 字符串是否非空:str==null 或者str为空串返回false;否则返回true * * @param str * @return */ public static final boolean isNotEmpty(String str) { return !isEmpty(str); } /** * 如果字符串是空或者是空串,则返回null;否则返回str.trim() * * @param str */ public static final String trimNull(String str) { return (isEmpty(str) ? null : str.trim()); } /** * Integer 转换成BigDecimal:如果integer==null,返回null;否则返回转换后的BigDecimal. * * @param integer * @return */ public static final BigDecimal convertIntegerToBigDecimal(Integer integer) { return (integer == null ? null : BigDecimal.valueOf(integer)); } /** * Long 转换成BigDecimal:如果Long==null,返回null;否则返回转换后的BigDecimal. * * @param Long * @return */ public static final BigDecimal convertLongToBigDecimal(Long para) { return (para == null ? null : BigDecimal.valueOf(para)); } /** * Integer 转换成BigInteger:如果integer==null,返回null;否则返回转换后的BigInteger. * * @param integer * @return */ public static final BigInteger convertIntegerToBigInteger(Integer integer) { return (integer == null ? null : BigInteger.valueOf(integer)); } /** * Short 转换成BigInteger:如果integer==null,返回null;否则返回转换后的BigInteger. * * @param integer * @return */ public static final BigInteger convertShortToBigInteger(Short source) { return (source == null ? null : BigInteger.valueOf(source)); } /** * BigInteger 转换成Integer:如果bigInteger==null,返回null;否则返回转换后的Integer. * * @param bigInteger * * @return */ public static final Integer convertBigIntegerToInteger(BigInteger bigInteger) { return (bigInteger == null ? null : bigInteger.intValue()); } public static final Integer convertBigDecimalToInteger(BigDecimal bigDecimal) { if (bigDecimal == null) { return null; } return bigDecimal.intValue(); } public static final Long StringToLong(String aStr) { Long longTemp = null; try { longTemp = new Long(aStr); } catch (Exception e) { log.warn("FormatUtil.StringToLong(" + aStr + ") failed with the errorMessage:" + e.getMessage()); } return longTemp; } public static final Integer convertLongToInteger(Long para) { return (para == null ? null : para.intValue()); } /** * 将字符串str截取成最大maxBytesLength的字2节,如果str字节数小于maxBytesLength,则返回原始str */ public static String subString(String str, int maxBytesLength) { if (isNotEmpty(str) && str.getBytes().length > maxBytesLength) { byte[] bytesOut = new byte[maxBytesLength]; byte[] bytesIn = str.getBytes(); System.arraycopy(bytesIn, 0, bytesOut, 0, maxBytesLength); str = new String(bytesOut); } return str; } /** * 将字符串str转化成double类型 * */ public static final Double convertStringToDouble(String str) { Double doubleTemp = 0.0; if (isEmpty(str)) { return doubleTemp; } else { doubleTemp = Double.parseDouble(str); return doubleTemp; } } /** * 将字符串str转化成Float类型 * */ public static final Float convertStringToFloat(String str) { Float floatTemp = new Float(0); if (isEmpty(str)) { return floatTemp; } else { floatTemp = Float.valueOf(str); return floatTemp; } } public static final Long convertBigDecimalToLong(BigDecimal bigDecimal) { String strTemp = convertBigDecimalToString(bigDecimal); Long longTemp = StringToLong(strTemp); return longTemp; } public static final BigInteger convertBigDecimalToBigInteger( BigDecimal bigDecimal) { Integer intTemp = convertBigDecimalToInteger(bigDecimal); BigInteger bigIntegerTemp = convertIntegerToBigInteger(intTemp); return bigIntegerTemp; } public static void main(String args[]) { String result = convertStringTo8DigitString("22111111"); System.out.println(result); } public static Map<String, Object> string2Map(String context) { if (context.length() <= 2) { return null; } Map<String, Object> map = new HashMap<String, Object>(); context = context.substring(1, context.length() - 1); String[] contextArray = context.split(","); for (String attribute : contextArray) { String[] attr = attribute.split(":"); if (attr.length == 2 && attr[1].length() > 2) { String value = attr[1].substring(1, attr[1].length() - 1); map.put(attr[0], value); } } return map; } }