123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- package com.hcks.cmfds.commons.util;
- import java.io.UnsupportedEncodingException;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.sql.Timestamp;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.UUID;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * @Author 刘厦(liusha.information@gmail.com)
- * @Date 创建时间:May 16, 2011 12:25:13 PM
- * @Version 0.0.0 类说明: 字符串操作工具类
- */
- public class StringUtils {
- public static final String SIMPLE_DATE_FORMAT = "yyyy-MM-dd";
- public static final String SIMPLE_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
- public static final String[] TRUE_STRINGS = new String[] { "true", "yes", "on", "1", "y" };
- /**
- *
- */
- public static final String REGEX_IMG_SRC = "src=\"(.*?)\"";
- /**
- * 转换中文参数
- *
- * @param str
- * @return
- */
- public static String convertEncoding(String str) {
- if (str == null)
- return str;
- try {
- return new String(str.getBytes("ISO-8859-1"), "UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- return str;
- }
- }
- /**
- * 获取字符串中所有图片的文件名
- *
- * @param memo
- * @return
- */
- public static List<String> getImg(String memo) {
- List<String> list = new ArrayList<String>();
- Pattern pa = Pattern.compile(REGEX_IMG_SRC, Pattern.DOTALL);
- Matcher matcher = pa.matcher(memo);
- while (matcher.find()) {
- list.add(matcher.group());
- }
- return list;
- }
- private StringUtils() {
- }
- /**
- * 串接字符串(通过 StringBuilder)
- *
- * @param strs
- * @return
- */
- public static String concat(String... strs) {
- if (strs == null)
- return null;
- StringBuilder buff = new StringBuilder();
- for (int i = 0; i < strs.length; i++) {
- buff.append(strs[i]);
- }
- return buff.toString();
- }
- /**
- * 是否为空串
- *
- * @param obj
- * @return
- */
- public static boolean empty(Object obj) {
- if (obj == null)
- return true;
- String str;
- if (obj instanceof String) {
- str = (String) obj;
- } else {
- str = obj.toString();
- }
- return str.length() == 0;
- }
- /**
- * 是否不为空串
- *
- * @param obj
- * @return
- */
- public static boolean notEmp(Object obj) {
- return !empty(obj);
- }
- /**
- * 转化为int 失败的话返回默认值
- *
- * @param str
- * @param defaultValue
- * @return
- */
- public static int toInt(String str, int defaultValue) {
- try {
- return Integer.parseInt(str);
- } catch (NumberFormatException e) {
- return defaultValue;
- }
- }
- public static int toInt(String str) {
- return toInt(str, 0);
- }
- public static float toFloat(String str) {
- return toFloat(str, 0);
- }
- /**
- * 转化为float 失败的话返回默认值
- *
- * @param str
- * @param defaultValue
- * @return
- */
- public static float toFloat(String str, float defaultValue) {
- try {
- return Float.parseFloat(str);
- } catch (NumberFormatException e) {
- return defaultValue;
- }
- }
- public static double toDouble(String str) {
- return toDouble(str, 0);
- }
- /**
- * 转化为double 失败的话返回默认值
- *
- * @param str
- * @param defaultValue
- * @return
- */
- public static double toDouble(String str, double defaultValue) {
- try {
- return Double.parseDouble(str);
- } catch (NumberFormatException e) {
- return defaultValue;
- }
- }
- public static long toLong(String str) {
- return toLong(str, 0);
- }
- /**
- * 转化为long 失败的话返回默认值
- *
- * @param str
- * @param defaultValue
- * @return
- */
- public static long toLong(String str, long defaultValue) {
- try {
- return Long.parseLong(str);
- } catch (NumberFormatException e) {
- return defaultValue;
- }
- }
- /**
- * 判断字符串"true" 是否表示 true
- *
- * @param str
- * @return
- */
- public static boolean isTrue(String str) {
- if (str == null || str.length() == 0)
- return false;
- for (int i = 0; i < TRUE_STRINGS.length; i++) {
- if (TRUE_STRINGS[i].equalsIgnoreCase(str)) {
- return true;
- }
- }
- return false;
- }
- public static boolean isFalse(String str) {
- return !isTrue(str);
- }
- /**
- * Timestamp转换为日期字符串
- *
- * @param ts
- * @return
- */
- public static String toDate(Timestamp ts) {
- if (ts == null)
- return "";
- SimpleDateFormat df = new SimpleDateFormat(SIMPLE_DATETIME_FORMAT);
- return df.format(ts);
- }
- public static Date formatTimestamp(Timestamp ts) {
- Date d1 = new Date(ts.getTime());
- String s1 = formatDate(d1, SIMPLE_DATETIME_FORMAT);
- return toDate(s1, SIMPLE_DATETIME_FORMAT);
- }
- /**
- * 将字符串解析为Date类型
- *
- * @param date
- * @param pattern
- * @return
- */
- public static Date toDate(String date, String pattern) {
- SimpleDateFormat format = new SimpleDateFormat(pattern);
- try {
- return format.parse(date);
- } catch (ParseException e) {
- throw new RuntimeException(String.format("Failed to parse the String [%s] to Date.", date), e);
- }
- }
- public static Date toDate(String date) {
- return toDate(date, SIMPLE_DATETIME_FORMAT);
- }
- /**
- * 格式化日期到字符串
- *
- * @param date
- * @param pattern
- * @return
- */
- public static String formatDate(Date date, String pattern) {
- if (date == null)
- return "";
- SimpleDateFormat format = new SimpleDateFormat(pattern);
- return format.format(date);
- }
- /**
- * 使用默认格式格式化日期到字符串
- *
- * @param date
- * @return
- */
- public static String formatDate(Date date) {
- SimpleDateFormat format = new SimpleDateFormat(SIMPLE_DATE_FORMAT);
- return format.format(date);
- }
- public static double round(double num, int digit) {
-
- Double d1 = new Double(num);
- if(d1.isNaN() || d1.isInfinite())
- {
- return 0.0;
- }
- return new BigDecimal(num).setScale(digit, RoundingMode.HALF_UP).doubleValue();
- }
- public static Double round(String num, int digit) {
-
- if(isNumeric(num))
- {
- Double d1 = new Double(num);
- if(d1.isNaN() || d1.isInfinite())
- {
- return 0.0;
- }
- return new BigDecimal(num).setScale(digit, RoundingMode.HALF_UP).doubleValue();
- }else
- {
- return null;
- }
-
- }
- public static int roundToInt(double num) {
-
- return new BigDecimal(num).setScale(0, RoundingMode.HALF_UP).intValue();
- }
- /**
- *
- * @方法名称: getUUID
- * @描述: 获得UUID
- * @参数 @return
- * @返回值 String
- * @抛出异常
- */
- public static String getUUID() {
- String s = UUID.randomUUID().toString();
- // 去掉“-”符号
- return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
- }
- public static String getFomatString(Double str)
- {
- if(StringUtils.notEmp(str))
- {
- java.text.DecimalFormat df =new java.text.DecimalFormat("0.00");
- return df.format(str);
- }else
- {
- return null;
- }
-
- }
- /**
- * 判断是否为数字字符串
- * @param str
- * @return
- */
- public static boolean isNumeric(String str){
- Pattern pattern = Pattern.compile("[0-9]*");
- if(notEmp(str))
- {
- Matcher isNum = pattern.matcher(str);
- if( !isNum.matches() ){
- return false;
- }
-
- }else
- {
- return false;
- }
-
- return true;
- }
- }
|