Explorar o código

日期解析空指针异常

kongwz %!s(int64=5) %!d(string=hai) anos
pai
achega
7c8e8722fe
Modificáronse 1 ficheiros con 251 adicións e 247 borrados
  1. 251 247
      public/src/main/java/com/lantone/qc/pub/util/StringUtil.java

+ 251 - 247
public/src/main/java/com/lantone/qc/pub/util/StringUtil.java

@@ -1,248 +1,252 @@
-package com.lantone.qc.pub.util;
-
-import com.lantone.qc.pub.Content;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.time.DateUtils;
-
-import java.text.ParseException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * @Description: 字符串有关帮助类 封装了第三方帮助类
- * @author: gaodm
- * @time: 2018/8/6 11:15
- */
-public class StringUtil {
-    /**
-     * 判断某字符串是否为空或长度为0或由空白符(whitespace) 构成
-     *
-     * @param str 需要判断的字符串
-     * @return
-     */
-    public static boolean isBlank(String str) {
-        return StringUtils.isBlank(str);
-    }
-
-    /**
-     * 判断某字符串是否不是为空或长度为0或由空白符(whitespace) 构成
-     *
-     * @param str 需要判断的字符串
-     * @return
-     */
-    public static boolean isNotBlank(String str) {
-        return StringUtils.isNotBlank(str);
-    }
-
-    /**
-     * 判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0
-     *
-     * @param str 需要判断的字符串
-     * @return
-     */
-    public static boolean isEmpty(String str) {
-        return StringUtils.isEmpty(str);
-    }
-
-    /**
-     * 判断某字符串是否不为空,为空的标准是 str==null 或 str.length()==0
-     *
-     * @param str 需要判断的字符串
-     * @return
-     */
-    public static boolean isNotEmpty(String str) {
-        return StringUtils.isNotEmpty(str);
-    }
-
-    /**
-     * 删除字符串中的换行和控制字符
-     *
-     * @param str
-     */
-    public static String remove_ctl(String str) {
-        return str.replaceAll("\r|\n|\r\n|/r/n", "").trim();
-    }
-
-    /**
-     * 清除字符串中的空白
-     *
-     * @param str
-     * @return
-     */
-    public static String removeBlank(String str) {
-        if (isBlank(str)) {
-            return str;
-        }
-        return str.replaceAll("[\\s\\p{Zs}]", "");
-    }
-
-    /**
-     * 比较两个列表的内容
-     */
-    public static List<String> compareList(List<String> A, List<String> B) {
-        List<String> res = new ArrayList<>();
-
-        for (String i : A) {
-            if (!B.contains(i)) {
-                res.add(i);
-            }
-        }
-
-        for (String j : B) {
-            if (!A.contains(j)) {
-                res.add(j);
-            }
-        }
-
-        return res;
-    }
-
-    /**
-     * 列表A是否包含列表B
-     */
-    public static boolean containList(List<String> A, List<String> B) {
-        boolean res = true;
-
-        try {
-            for (String item : B) {
-                if (!A.contains(item)) {
-                    res = false;
-                    break;
-                }
-            }
-        } catch (Exception ex) {
-            ex.printStackTrace();
-        } finally {
-            return res;
-        }
-    }
-
-    /**
-     * 比较两个字符串集合是否一模一样:个数一样、顺序一样
-     *
-     * @param source
-     * @param target
-     * @return
-     */
-    public static boolean isEqually(List<String> source, List<String> target) {
-        boolean ret = false;
-        if (ListUtil.isNotEmpty(source) && ListUtil.isNotEmpty(target)) {
-            String[] sourceArray = source.toArray(new String[] {});
-            String[] targetArray = target.toArray(new String[] {});
-            ret = Arrays.equals(sourceArray, targetArray);
-        }
-        return ret;
-    }
-
-    /**
-     * 判断字符串是否包含数字
-     *
-     * @param company
-     * @return
-     */
-    public static boolean isContainNumber(String company) {
-        Pattern p = Pattern.compile("[0-9]");
-        Matcher m = p.matcher(company);
-        if (m.find()) {
-            return true;
-        }
-        return false;
-    }
-
-    /**
-     * 标点符号空格等转逗号分割
-     *
-     * @param content
-     * @return
-     */
-    public static String specialCharComma(String content) {
-        if (StringUtil.isNotBlank(content)) {
-            content = content.replaceAll("(\\d+)|[、,。.;;??]", " ").replaceAll("(\\s+)", ",");
-            if (content.indexOf(",") == 0) {
-                content = content.substring(1);
-            }
-            if (content.lastIndexOf(",") == content.length() - 1) {
-                content = content.substring(0, content.length() - 1);
-            }
-        }
-        return content;
-    }
-
-    /**
-     * 判断两个字符串是否相同
-     *
-     * @param arg1
-     * @param arg2
-     * @return
-     */
-    public static boolean equals(String arg1, String arg2) {
-        return StringUtils.equals(arg1, arg2);
-    }
-
-    /**
-     * 比较两个字符串集合是否内容一样,即A包含B,B包含A,交集为空
-     * 个数、顺序不考虑
-     *
-     * @param source
-     * @param target
-     * @return
-     */
-//    public static boolean isSameContent(List<String> source, List<String> target) {
-//        Set<String> sourceSet = Sets.newHashSet(source);
-//        Set<String> targetSet = Sets.newHashSet(target);
-//        return Sets.difference(sourceSet, targetSet).isEmpty() && Sets.difference(targetSet, sourceSet).isEmpty();
-//    }
-
-    /**
-     * 解析时间
-     *
-     * @param datetime
-     * @return
-     */
-    public static Date parseDateTime(String datetime) {
-        Date date = null;
-        try {
-            datetime = remove_ctl(datetime);
-
-            if (datetime.contains("至")) {
-                datetime = datetime.split("至")[1].replaceAll("[\\u4e00-\\u9fa5]", "");
-            }
-
-            if (datetime.length() > 0) {
-                date = DateUtils.parseDate(datetime, Content.dateFormats);
-            }
-        } catch (ParseException ex) {
-            ex.printStackTrace();
-        }
-        return date;
-    }
-
-    /**
-     * 根据给定的时间格式解析时间
-     *
-     * @param datetime
-     * @return
-     */
-    public static Date parseDateTime(String datetime,String[] dateFormats) {
-        Date date = null;
-        try {
-            datetime = remove_ctl(datetime);
-
-            if (datetime.contains("至")) {
-                datetime = datetime.split("至")[1].replaceAll("[\\u4e00-\\u9fa5]", "");
-            }
-            if (datetime.length() > 0) {
-                date = DateUtils.parseDate(datetime, dateFormats);
-            }
-        } catch (ParseException ex) {
-            ex.printStackTrace();
-        } finally {
-            return date;
-        }
-    }
-
+package com.lantone.qc.pub.util;
+
+import com.lantone.qc.pub.Content;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.time.DateUtils;
+
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @Description: 字符串有关帮助类 封装了第三方帮助类
+ * @author: gaodm
+ * @time: 2018/8/6 11:15
+ */
+public class StringUtil {
+    /**
+     * 判断某字符串是否为空或长度为0或由空白符(whitespace) 构成
+     *
+     * @param str 需要判断的字符串
+     * @return
+     */
+    public static boolean isBlank(String str) {
+        return StringUtils.isBlank(str);
+    }
+
+    /**
+     * 判断某字符串是否不是为空或长度为0或由空白符(whitespace) 构成
+     *
+     * @param str 需要判断的字符串
+     * @return
+     */
+    public static boolean isNotBlank(String str) {
+        return StringUtils.isNotBlank(str);
+    }
+
+    /**
+     * 判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0
+     *
+     * @param str 需要判断的字符串
+     * @return
+     */
+    public static boolean isEmpty(String str) {
+        return StringUtils.isEmpty(str);
+    }
+
+    /**
+     * 判断某字符串是否不为空,为空的标准是 str==null 或 str.length()==0
+     *
+     * @param str 需要判断的字符串
+     * @return
+     */
+    public static boolean isNotEmpty(String str) {
+        return StringUtils.isNotEmpty(str);
+    }
+
+    /**
+     * 删除字符串中的换行和控制字符
+     *
+     * @param str
+     */
+    public static String remove_ctl(String str) {
+        String trim = "";
+        if(StringUtils.isNotEmpty(str)){
+            trim = str.replaceAll("\r|\n|\r\n|/r/n", "").trim();
+        }
+        return trim;
+    }
+
+    /**
+     * 清除字符串中的空白
+     *
+     * @param str
+     * @return
+     */
+    public static String removeBlank(String str) {
+        if (isBlank(str)) {
+            return str;
+        }
+        return str.replaceAll("[\\s\\p{Zs}]", "");
+    }
+
+    /**
+     * 比较两个列表的内容
+     */
+    public static List<String> compareList(List<String> A, List<String> B) {
+        List<String> res = new ArrayList<>();
+
+        for (String i : A) {
+            if (!B.contains(i)) {
+                res.add(i);
+            }
+        }
+
+        for (String j : B) {
+            if (!A.contains(j)) {
+                res.add(j);
+            }
+        }
+
+        return res;
+    }
+
+    /**
+     * 列表A是否包含列表B
+     */
+    public static boolean containList(List<String> A, List<String> B) {
+        boolean res = true;
+
+        try {
+            for (String item : B) {
+                if (!A.contains(item)) {
+                    res = false;
+                    break;
+                }
+            }
+        } catch (Exception ex) {
+            ex.printStackTrace();
+        } finally {
+            return res;
+        }
+    }
+
+    /**
+     * 比较两个字符串集合是否一模一样:个数一样、顺序一样
+     *
+     * @param source
+     * @param target
+     * @return
+     */
+    public static boolean isEqually(List<String> source, List<String> target) {
+        boolean ret = false;
+        if (ListUtil.isNotEmpty(source) && ListUtil.isNotEmpty(target)) {
+            String[] sourceArray = source.toArray(new String[] {});
+            String[] targetArray = target.toArray(new String[] {});
+            ret = Arrays.equals(sourceArray, targetArray);
+        }
+        return ret;
+    }
+
+    /**
+     * 判断字符串是否包含数字
+     *
+     * @param company
+     * @return
+     */
+    public static boolean isContainNumber(String company) {
+        Pattern p = Pattern.compile("[0-9]");
+        Matcher m = p.matcher(company);
+        if (m.find()) {
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * 标点符号空格等转逗号分割
+     *
+     * @param content
+     * @return
+     */
+    public static String specialCharComma(String content) {
+        if (StringUtil.isNotBlank(content)) {
+            content = content.replaceAll("(\\d+)|[、,。.;;??]", " ").replaceAll("(\\s+)", ",");
+            if (content.indexOf(",") == 0) {
+                content = content.substring(1);
+            }
+            if (content.lastIndexOf(",") == content.length() - 1) {
+                content = content.substring(0, content.length() - 1);
+            }
+        }
+        return content;
+    }
+
+    /**
+     * 判断两个字符串是否相同
+     *
+     * @param arg1
+     * @param arg2
+     * @return
+     */
+    public static boolean equals(String arg1, String arg2) {
+        return StringUtils.equals(arg1, arg2);
+    }
+
+    /**
+     * 比较两个字符串集合是否内容一样,即A包含B,B包含A,交集为空
+     * 个数、顺序不考虑
+     *
+     * @param source
+     * @param target
+     * @return
+     */
+//    public static boolean isSameContent(List<String> source, List<String> target) {
+//        Set<String> sourceSet = Sets.newHashSet(source);
+//        Set<String> targetSet = Sets.newHashSet(target);
+//        return Sets.difference(sourceSet, targetSet).isEmpty() && Sets.difference(targetSet, sourceSet).isEmpty();
+//    }
+
+    /**
+     * 解析时间
+     *
+     * @param datetime
+     * @return
+     */
+    public static Date parseDateTime(String datetime) {
+        Date date = null;
+        try {
+            datetime = remove_ctl(datetime);
+
+            if (datetime.contains("至")) {
+                datetime = datetime.split("至")[1].replaceAll("[\\u4e00-\\u9fa5]", "");
+            }
+
+            if (datetime.length() > 0) {
+                date = DateUtils.parseDate(datetime, Content.dateFormats);
+            }
+        } catch (ParseException ex) {
+            ex.printStackTrace();
+        }
+        return date;
+    }
+
+    /**
+     * 根据给定的时间格式解析时间
+     *
+     * @param datetime
+     * @return
+     */
+    public static Date parseDateTime(String datetime,String[] dateFormats) {
+        Date date = null;
+        try {
+            datetime = remove_ctl(datetime);
+
+            if (datetime.contains("至")) {
+                datetime = datetime.split("至")[1].replaceAll("[\\u4e00-\\u9fa5]", "");
+            }
+            if (datetime.length() > 0) {
+                date = DateUtils.parseDate(datetime, dateFormats);
+            }
+        } catch (ParseException ex) {
+            ex.printStackTrace();
+        } finally {
+            return date;
+        }
+    }
+
 }