package com.moral.common.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class StringUtils { public static final char UNDERLINE = '_'; /** * 驼峰格式字符串转换为下划线格式字符串 * * @param param * @return */ public static String camelToUnderline(String param) { if (param == null || "".equals(param.trim())) { return ""; } int len = param.length(); StringBuilder sb = new StringBuilder(len); for (int i = 0; i < len; i++) { char c = param.charAt(i); if (Character.isUpperCase(c)) { sb.append(UNDERLINE); sb.append(Character.toLowerCase(c)); } else { sb.append(c); } } return sb.toString(); } /** * //首字母转大写 * * @param s * @return */ public static String toUpperCaseFirstOne(String s){ StringBuilder sb = new StringBuilder(s); sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); return sb.toString(); } public static boolean isNullOrEmpty(String toTest) { return toTest == null || toTest.length() == 0; } public static Object stringToObject4Type(Class type,String value) throws Exception{ Object result = value; if (type == double.class || type == Double.class) { result = Double.parseDouble(value); } else if (type == float.class || type == Float.class) { result = Float.parseFloat(value); } else if (type == long.class || type == Long.class) { result = Long.parseLong(value); } else if (type == int.class || type == Integer.class) { result = Integer.parseInt(value); } else if (type == short.class || type == Short.class) { result = Short.parseShort(value); } else if (type == boolean.class || type == Boolean.class) { result = Boolean.parseBoolean(value); } else if (type == Date.class) { value = value.replace("T", " "); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { result = df.parse(value); } catch (Exception e) { df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); try { result = df.parse(value); } catch (ParseException e1) { df = new SimpleDateFormat("yyyy-MM-dd"); try { result = df.parse(value); } catch (ParseException e2) { } } } } return result; } }