字符串工具类

package **.tool;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringTool {
    //判断String类型是否为数字
    //java自带函数
    public static boolean isNumerA(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断身份证格式
     *
     * @param idNum 身份证号
     * @return 结果
     */
    public static boolean isIdNum(String idNum) {
        // 中国公民身份证格式:长度为15或18位,最后一位可以为字母
        Pattern idNumPattern = Pattern.compile("(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])");
        // 格式验证
        if (!idNumPattern.matcher(idNum).matches())
            return false;
        // 合法性验证
        int year = 0;
        int month = 0;
        int day = 0;
        if (idNum.length() == 15) {
            // 提取身份证上的前6位以及出生年月日
            Pattern birthDatePattern = Pattern.compile("\\d{6}(\\d{2})(\\d{2})(\\d{2}).");
            Matcher birthDateMather = birthDatePattern.matcher(idNum);
            if (birthDateMather.find()) {
                year = Integer.valueOf("19" + birthDateMather.group(1));
                month = Integer.valueOf(birthDateMather.group(2));
                day = Integer.valueOf(birthDateMather.group(3));
            }
        } else if (idNum.length() == 18) {
            // 提取身份证上的前6位以及出生年月日
            Pattern birthDatePattern = Pattern.compile("\\d{6}(\\d{4})(\\d{2})(\\d{2}).*");
            Matcher birthDateMather = birthDatePattern.matcher(idNum);
            if (birthDateMather.find()) {
                year = Integer.valueOf(birthDateMather.group(1));
                month = Integer.valueOf(birthDateMather.group(2));
                day = Integer.valueOf(birthDateMather.group(3));
            }
        }
        // 年份判断,100年前至今
        Calendar cal = Calendar.getInstance();
        // 当前年份
        int currentYear = cal.get(Calendar.YEAR);
        if (year <= currentYear - 100 || year > currentYear)
            return false;
        // 月份判断
        if (month < 1 || month > 12)
            return false;
        // 日期判断
        // 计算月份天数
        int dayCount = 31;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                dayCount = 31;
                break;
            case 2:
                // 2月份判断是否为闰年
                if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                    dayCount = 29;
                    break;
                } else {
                    dayCount = 28;
                    break;
                }
            case 4:
            case 6:
            case 9:
            case 11:
                dayCount = 30;
                break;
        }
        return day >= 1 && day <= dayCount;
    }

    public static boolean isEmpty(String str) {
        if (null == str || "".equals(str.trim()) || "]".equals(str)) {
            return true;
        }
        return false;
    }

    @SuppressWarnings("unused")
    public static String clobToString(Clob clob) {
        if (clob == null) {
            return null;
        }
        try {
            Reader inStreamDoc = clob.getCharacterStream();
            char[] tempDoc = new char[(int) clob.length()];
            inStreamDoc.read(tempDoc);
            inStreamDoc.close();
            return new String(tempDoc);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException es) {
            es.printStackTrace();
        }

        return null;
    }

    public static String getGuid() {
        return UUID.randomUUID().toString().replace("-", "").toUpperCase();
    }

    public static Timestamp getNowTime() {
        Timestamp now = new Timestamp(System.currentTimeMillis());
        return now;
    }

    public static String getCurrentDateStr(String format) {
        if (isEmpty(format)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }

    public static Date stringToDate(String str) {
        SimpleDateFormat sdf = null;
        if (str.length() > 11)
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (str.length() < 11)
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String timeToString(Timestamp time) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        str = sdf.format(time);
        return str;
    }

    public static String mapToString(Map<String, Object> map) {
        StringBuffer sb = new StringBuffer();
        sb.append("{");
        for (String key : map.keySet()) {
            sb.append("\"" + key + "\":\"" + map.get(key) + "\",");
        }
        int a = sb.length();
        sb.deleteCharAt(a - 1);
        sb.append("}");
        return sb.toString();
    }

    public static String mapToJson(String str) {
        String first = str.replace("=", "\":\"");
        String second = first.replace(", ", "\",\"");
        String third = second.replace("{", "{\"");
        String fourth = third.replace("}", "\"}");
        return fourth;
    }

    public static Map<String, Object> getCondition(String condition, String date) throws IOException {
        String sql = " ";

        List listValue = null;
        if (condition != null && date != null) {
            listValue = new ArrayList();
            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> conditionMap = (Map<String, Object>) mapper.readValue(condition, Map.class);

            if (conditionMap.containsKey("startDate")) {
                sql = sql + " and " + date + ">=to_date(? ,'yyyy-mm-dd hh24-mi-ss') ";
                listValue.add(conditionMap.get("startDate"));
                conditionMap.remove("startDate");
            }

            if (conditionMap.containsKey("endDate")) {
                sql = sql + " and " + date + "<=to_date(? ,'yyyy-mm-dd hh24-mi-ss') ";
                listValue.add(conditionMap.get("endDate"));
                conditionMap.remove("endDate");
            }

            if (conditionMap.size() > 0) {
                for (Map.Entry<String, Object> stringObjectEntry : conditionMap.entrySet()) {
                    sql = sql + " and instr(" + (String) stringObjectEntry.getKey() + ",?)>0";
                    listValue.add(stringObjectEntry.getValue());
                }
            }
            Map<String, Object> result = new TreeMap<>();
            result.put("sql", sql);
            result.put("value", listValue);
            return result;
        }

        return null;
    }

    public static String objectToStr(Object object) {
        if (object == null) {
            return "";
        }
        return object.toString();
    }

    /**
     * 将List<String>集合 转化为String
     * 如{"aaa","bbb"} To 'aaa','bbb'
     */
    public static String convertListToString(List<String> strlist) {
        StringBuffer sb = new StringBuffer();
        if (CollectionUtils.isNotEmpty(strlist)) {
            for (int i = 0; i < strlist.size(); i++) {
                if (i == 0) {
                    sb.append("'").append(strlist.get(i)).append("'");
                } else {
                    sb.append(",").append("'").append(strlist.get(i)).append("'");
                }
            }
        }
        return sb.toString();
    }


    /***
     * 将"1,2,3,4,5..."这种形式的字符串转成List<String> 集合
     * @param strs
     * @return
     * */

    public static List<String> converStringToList(String strs) {
        if (StringUtils.isNotBlank(strs)) {
            String[] idStrs = strs.trim().split(",");
            if (null != idStrs && idStrs.length > 0) {
                List<String> strsList = new ArrayList<String>();
                for (String str : idStrs) {
                    if (StringUtils.isNotBlank(str)) {
                        strsList.add(str.trim());
                    }
                }
                if (strsList.size() > 0) {
                    return strsList;
                }
            }
        }
        return null;
    }

    /**
     * 将"1,2,3,4,5..."这种形式的字符串转成"'1','2','3','4'..."这种形式
     *
     * @param strs
     * @return
     */
    public static String converString(String strs) {
        if (StringUtils.isNotBlank(strs)) {
            String[] idStrs = strs.trim().split(",");
            if (null != idStrs && idStrs.length > 0) {
                StringBuffer sbf = new StringBuffer("");
                for (String str : idStrs) {
                    if (StringUtils.isNotBlank(str)) {
                        sbf.append("'").append(str.trim()).append("'").append(",");
                    }
                }
                if (sbf.length() > 0) {
                    sbf = sbf.deleteCharAt(sbf.length() - 1);
                    return sbf.toString();
                }
            }
        }
        return "";
    }

    /*public static String cachedSetToString(CachedSetPageInfo cachedSetPageInfo) {
        String s = "";
        if (cachedSetPageInfo.getCItems() != null) {
            s = s + "{\"currentPage\":" + cachedSetPageInfo.getCurrentPage() + ",";
            s = s + "\"totalPages\":" + cachedSetPageInfo.getTotalPages() + ",";
            s = s + "\"totalItems\":" + cachedSetPageInfo.getTotalItems() + ",";
            s = s + "\"itemsPrePage\":" + cachedSetPageInfo.getItemsPrePage() + ",";
            s = s + "\"items\":";
            String result = JsonHelper.CachedRowSetToJson(cachedSetPageInfo.getCItems());
            if (result.equals("]")) {
                return "";
            }
            s = s + result;
            s = s + "}";
        }
        return s;
    }*/
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值