|
@@ -0,0 +1,220 @@
|
|
|
+/*
|
|
|
+ * 创建时间:2017-09-03 21:37
|
|
|
+ * 项目名称:kmall_pt
|
|
|
+ * 类名称:StringUtils.java
|
|
|
+ * 包名称:com.kmall.common.utils
|
|
|
+ */
|
|
|
+package com.ematou.wxbase.utils;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 名称:StringUtils <br>
|
|
|
+ * 描述:String工具类<br>
|
|
|
+ *
|
|
|
+ * @author Scott
|
|
|
+ * @version 1.0
|
|
|
+ * @since 1.0.0
|
|
|
+ */
|
|
|
+public class StringUtils {
|
|
|
+ public static final String EMPTY = "";
|
|
|
+ private static Pattern linePattern = Pattern.compile("_(\\w)");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断字符串是否不为空,不为空则返回true
|
|
|
+ *
|
|
|
+ * @param str 源数据
|
|
|
+ * @return Boolean
|
|
|
+ */
|
|
|
+ public static boolean isNotEmpty(String str) {
|
|
|
+ if (str != null && !"".equals(str.trim()) && !"null".equalsIgnoreCase(str)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断对象或对象数组中每一个对象是否为空: 对象为null,字符序列长度为0,集合类、Map为empty
|
|
|
+ *
|
|
|
+ * @param obj
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isNullOrEmpty(Object obj) {
|
|
|
+ if (obj == null) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (obj instanceof CharSequence) {
|
|
|
+ return ((CharSequence) obj).length() == 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (obj instanceof Collection) {
|
|
|
+ return ((Collection) obj).isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (obj instanceof Map) {
|
|
|
+ return ((Map) obj).isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (obj instanceof Object[]) {
|
|
|
+ Object[] object = (Object[]) obj;
|
|
|
+ if (object.length == 0) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ boolean empty = true;
|
|
|
+ for (int i = 0; i < object.length; i++) {
|
|
|
+ if (!isNullOrEmpty(object[i])) {
|
|
|
+ empty = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return empty;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下划线转驼峰
|
|
|
+ */
|
|
|
+ public static String lineToHump(String str) {
|
|
|
+ str = str.toLowerCase();
|
|
|
+ Matcher matcher = linePattern.matcher(str);
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ while (matcher.find()) {
|
|
|
+ matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
|
|
|
+ }
|
|
|
+ matcher.appendTail(sb);
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验数值只能是数字且可以带两位小数
|
|
|
+ * @param value
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean checkNumberByTwoDecimal(String value){
|
|
|
+ // 要验证的字符串
|
|
|
+ String str = String.valueOf(value);
|
|
|
+ // 带小数
|
|
|
+ String regEx = "\\d+(\\.\\d{1,2})?";
|
|
|
+ // 编译正则表达式
|
|
|
+ Pattern pattern = Pattern.compile(regEx);
|
|
|
+ Matcher matcher = pattern.matcher(str);
|
|
|
+ // 字符串是否与正则表达式相匹配
|
|
|
+ boolean rs = matcher.matches();
|
|
|
+ return rs;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 判断是否为整数
|
|
|
+ * @param str 传入的字符串
|
|
|
+ * @return 是整数返回true,否则返回false
|
|
|
+ */
|
|
|
+ public static boolean isInteger(String str) {
|
|
|
+ Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
|
|
|
+ return pattern.matcher(str).matches();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验数值只能是数字且只可以带一位小数
|
|
|
+ * @param value
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean checkNumberByOneDecimal(String value){
|
|
|
+ // 要验证的字符串
|
|
|
+ String str = String.valueOf(value);
|
|
|
+ // 带小数
|
|
|
+ String regEx = "\\d+(\\.\\d{1,1})?";
|
|
|
+ // 编译正则表达式
|
|
|
+ Pattern pattern = Pattern.compile(regEx);
|
|
|
+ Matcher matcher = pattern.matcher(str);
|
|
|
+ // 字符串是否与正则表达式相匹配
|
|
|
+ boolean rs = matcher.matches();
|
|
|
+ return rs;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验数值只能是数字
|
|
|
+ * @param value
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean checkNumberByInteger(String value){
|
|
|
+ // 要验证的字符串
|
|
|
+ String str = String.valueOf(value);
|
|
|
+ // 带小数
|
|
|
+ String regEx = "[+]{0,1}(\\d+)";
|
|
|
+ // 编译正则表达式
|
|
|
+ Pattern pattern = Pattern.compile(regEx);
|
|
|
+ Matcher matcher = pattern.matcher(str);
|
|
|
+ // 字符串是否与正则表达式相匹配
|
|
|
+ boolean rs = matcher.matches();
|
|
|
+ return rs;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取字符串的长度,如果有中文,则每个中文字符计为2位
|
|
|
+ *
|
|
|
+ * @param value
|
|
|
+ * 指定的字符串
|
|
|
+ *
|
|
|
+ * @return 字符串的长度
|
|
|
+ */
|
|
|
+ public static int length(String value) {
|
|
|
+ int valueLength = 0;
|
|
|
+ String chinese = "[\u0391-\uFFE5]";
|
|
|
+ /* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */
|
|
|
+ for (int i = 0; i < value.length(); i++) {
|
|
|
+ /* 获取一个字符 */
|
|
|
+ String temp = value.substring(i, i + 1);
|
|
|
+ /* 判断是否为中文字符 */
|
|
|
+ if (temp.matches(chinese)) {
|
|
|
+ /* 中文字符长度为2 */
|
|
|
+ valueLength += 2;
|
|
|
+ } else {
|
|
|
+ /* 其他字符长度为1 */
|
|
|
+ valueLength += 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return valueLength;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据身份证计算年龄
|
|
|
+ * @param idCard
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Integer countAge(String idCard) {
|
|
|
+ if (idCard.length() != 18 && idCard.length() != 15) {
|
|
|
+ throw new IllegalArgumentException("身份证号长度错误");
|
|
|
+ }
|
|
|
+ String year;
|
|
|
+ String monthDay;
|
|
|
+ if (idCard.length() == 18) {
|
|
|
+ year = idCard.substring(6,10);
|
|
|
+ monthDay = idCard.substring(10,14);
|
|
|
+ } else {
|
|
|
+ year = "19" + idCard.substring(6, 8);
|
|
|
+ monthDay = idCard.substring(8, 12);
|
|
|
+ }
|
|
|
+ //获取当前时间字符串如:2022-1128
|
|
|
+ String nowTimeStr = new SimpleDateFormat("yyyy-MMdd").format(new Date());
|
|
|
+ String yearNow = nowTimeStr.substring(0, 4);// 当前年份
|
|
|
+ String monthDayNow = nowTimeStr.substring(5, 9);// 当前月日
|
|
|
+ int age = Integer.parseInt(yearNow) - Integer.parseInt(year);
|
|
|
+ //age减一的情况 :用户月日大于当前月日(开头可以为0的4位数int)
|
|
|
+ if (Integer.parseInt(monthDay) > Integer.parseInt(monthDayNow)) {
|
|
|
+ age = age - 1;
|
|
|
+ }
|
|
|
+ return age;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|