package com.jsjty.util; import java.io.PrintWriter; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Date; import java.util.Map; import java.util.UUID; import javax.servlet.http.HttpServletRequest; public class StringUtil { private final static SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); public static String formatDate(Date date) { return dateformat.format(date); } public static String formatTimestamp(Timestamp timestamp) { return dateformat.format(timestamp); } /** * 获取错误日志详细信息 * * @param t * @return String */ public static String getTrace(Throwable t) { StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); t.printStackTrace(writer); StringBuffer buffer = stringWriter.getBuffer(); return buffer.toString(); } public static String[] str2Array(String params) { if (params != null) { return params.split(","); } return null; } /** * 对文件流输出下载的中文文件名进行编码 屏蔽各种浏览器版本的差异性 */ public static String encodeChineseDownloadFileName(HttpServletRequest request, String pFileName) { String agent = request.getHeader("USER-AGENT"); try { if (null != agent && -1 != agent.indexOf("MSIE")) { pFileName = URLEncoder.encode(pFileName, "utf-8"); } else { pFileName = new String(pFileName.getBytes("utf-8"), "iso8859-1"); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return pFileName; } /** * 判断对象是否Empty(null或元素为0)
* 实用于对如下对象做判断:String Collection及其子类 Map及其子类 * * @param pObj * 待检查对象 * @return boolean 返回的布尔值 */ @SuppressWarnings("rawtypes") public static boolean isEmpty(Object pObj) { if (pObj == null) return true; if (pObj == "") return true; if (pObj instanceof String) { if (((String) pObj).length() == 0) { return true; } } else if (pObj instanceof Collection) { if (((Collection) pObj).size() == 0) { return true; } } else if (pObj instanceof Map) { if (((Map) pObj).size() == 0) { return true; } } return false; } /** * 判断对象是否为NotEmpty(!null或元素>0)
* 实用于对如下对象做判断:String Collection及其子类 Map及其子类 * * @param pObj * 待检查对象 * @return boolean 返回的布尔值 */ @SuppressWarnings("rawtypes") public static boolean isNotEmpty(Object pObj) { if (pObj == null) return false; if (pObj == "") return false; if (pObj instanceof String) { if (((String) pObj).length() == 0) { return false; } } else if (pObj instanceof Collection) { if (((Collection) pObj).size() == 0) { return false; } } else if (pObj instanceof Map) { if (((Map) pObj).size() == 0) { return false; } } return true; } public static String getUUID() { return UUID.randomUUID().toString().replace("-", ""); } public static String parseByte2HexStr(byte buf[]) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < buf.length; i++) { String hex = Integer.toHexString(buf[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } return sb.toString(); } /** * 把16进制字符串转换成字节数组 * * @param hexString * @return byte[] */ public static byte[] hexStringToByte(String hex) { int len = (hex.length() / 2); byte[] result = new byte[len]; char[] achar = hex.toCharArray(); for (int i = 0; i < len; i++) { int pos = i * 2; result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1])); } return result; } private static int toByte(char c) { byte b = (byte) "0123456789ABCDEF".indexOf(c); return b; } }