平常開發中常常會遇到一些經常使用頻繁的數據類型轉換、日期格式轉換、非空校驗、避免重複造輪子寫代碼通常咱們通常會封裝一個經常使用的Utils開放工具類;java
最近在開發中遇到數組、list、string的轉換比較頻繁,公司的原有的工具類無法知足因此對原有的工具類進行修改,爲了後面其餘項目也能引用將原有工具類進行了優化:正則表達式
UtilsTools.java數組
import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class UtilsTools { /**判斷傳入值是否爲空*/ public static boolean isEmpty(String str) { if ( str == null || "".equals(str) || "null".equals(str) || "undefined".equals(str)) { return true; } else { return false; } } /**判斷傳入值是否不爲空*/ public static boolean isNotEmpty(String str) { if (!"".equals(str) && str != null) { return true; } else { return false; } } /** * String字符串轉數字數組 * @param ids:傳入的數組字符串 * @param Separator:分隔符支持','及'&'等自定義分隔符 * */ public static Integer[] StrToArray(String ids,String Separator){ try { List<Integer> list = new ArrayList<Integer>(); String[] strs = ids.split(Separator); for (String id : strs) { if(!UtilsTools.isEmpty(id)){ Integer id1 = Integer.parseInt(id); if(!list.contains(id1)){ list.add(id1); } } } return list.toArray(new Integer[list.size()]); } catch (RuntimeException e) { throw e; } } /** * String字符串轉String數組 * @param ids:傳入的數組字符串 * @param Separator:分隔符支持','及'&'等自定義分隔符 * */ public static String[] StrToArrayString(String ids,String Separator){ try { String[] strs = ids.split(Separator); return strs; } catch (RuntimeException e) { throw e; } } /** * 字符串直接轉List<Integer> * @param ids:傳入的數組字符串 * @param Separator:分隔符支持','及'&'等自定義分隔符 * */ public static List<Integer> StrToIntegerList(String ids,String Separator){ try { List<Integer> list = new ArrayList<Integer>(); String[] strs = ids.split(Separator); for (String id : strs) { if(!UtilsTools.isEmpty(id)){ Integer id1 = Integer.parseInt(id); if(!list.contains(id1)){ list.add(id1); } } } return list; } catch (RuntimeException e) { throw e; } } /** * 字符串直接轉List<String> * @param ids:傳入的數組字符串 * @param Separator:分隔符支持','及'&'等自定義分隔符 * */ public static List<String> StrToStringList(String ids,String Separator){ try { List<String> list = new ArrayList<String>(); String[] strs = ids.split(Separator); for (String str : strs) { if(!UtilsTools.isEmpty(str)){ if(!list.contains(str)){ list.add(str); } } } return list; } catch (RuntimeException e) { throw e; } } /** * List<String>直轉String * List<String>轉帶自定義分隔符字符串 * @param list:傳入的List集合,內部參數String * @param Separator:分隔符支持','及'&'等自定義分隔符 * */ public static String StringListToStr(List<String> list,String Separator){ String strs=""; try { for (String str : list) { strs+=str+Separator; } //截取最後一位多餘的分割符號(取分割符長度,長度大於1的分割符一樣有效) strs=strs.substring(0, strs.length()-Separator.length()); return strs; } catch (RuntimeException e) { throw e; } } /** * List<Integer>直轉String * List<Integer>轉帶自定義分隔符字符串 * @param list:傳入的List集合,內部參數Integer * @param Separator:分隔符支持','及'&'等自定義分隔符 * */ public static String IntgerListToStr(List<Integer> list,String Separator){ String strs=""; try { for (Integer str : list) { strs+=str.toString()+Separator; } //截取最後一位多餘的分割符號(取分割符長度,長度大於1的分割符一樣有效) strs=strs.substring(0, strs.length()-Separator.length()); return strs; } catch (RuntimeException e) { throw e; } } /** * String[]直轉String * String[]轉帶自定義分隔符字符串 * @param String[]:傳入的String[] * @param Separator:分隔符支持','及'&'等自定義分隔符 * */ public static String IntgerArrayToStr(String[] array,String Separator){ String strs=""; try { for (String str : array) { strs+=str+Separator; } //截取最後一位多餘的分割符號(取分割符長度,長度大於1的分割符一樣有效) strs=strs.substring(0, strs.length()-Separator.length()); return strs; } catch (RuntimeException e) { throw e; } } /** * Integer[]直轉String * Integer[]轉帶自定義分隔符字符串 * @param Integer[]:傳入的Integer[] * @param Separator:分隔符支持','及'&'等自定義分隔符 * */ public static String IntgerArrayToStr(Integer[] array,String Separator){ String strs=""; try { for (Integer str : array) { strs+=str.toString()+Separator; } //截取最後一位多餘的分割符號(取分割符長度,長度大於1的分割符一樣有效) strs=strs.substring(0, strs.length()-Separator.length()); return strs; } catch (RuntimeException e) { throw e; } } /** * ','分割的字符串轉Integer數組 */ public static Integer[] getInts(String ids){ try { List<Integer> list = new ArrayList<Integer>(); String[] strs = ids.split(","); for (String id : strs) { if(!UtilsTools.isEmpty(id)){ Integer id1 = Integer.parseInt(id); if(!list.contains(id1)){ list.add(id1); } } } return list.toArray(new Integer[list.size()]); } catch (NumberFormatException e) { throw e; } } //補零 public static String fillZero(Integer num){ if(num<10){ return "0"+num; } return ""+num; } /** * 將長時間格式字符串轉換爲時間 yyyy-MM-dd HH:mm * * @param strDate * @return */ public static Date toDate(String strDate) { if(isEmpty(strDate)){ return null; } SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } /** * 將長時間格式字符串轉換爲時間 yyyy-MM-dd * * @param strDate * @return */ public static Date toSortDate(String strDate) { if(isEmpty(strDate)){ return null; } SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } public static String toDateString(Date date) { if (date == null) { return ""; } SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); return formatter.format(date); } public static String toLongDateHzString(Date date) { if (date == null) { return ""; } SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm"); return formatter.format(date); } public static String toShortDateHzString(Date date) { if (date == null) { return ""; } SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日"); return formatter.format(date); } public static String toDateFullString(Date date) { if (date == null) { return ""; } SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return formatter.format(date); } public static String toShortDateString(Date date) { if (date == null) { return ""; } SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日"); return formatter.format(date); } public static String toShortDateTimeString(Date date) { if (date == null) { return ""; } SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日 HH:mm"); return formatter.format(date); } public static String toTimeString(Date date) { if (date == null) { return ""; } SimpleDateFormat formatter = new SimpleDateFormat("HH:mm"); return formatter.format(date); } public static String getEncoding(String str) { String encode = "GB2312"; try { if (str.equals(new String(str.getBytes(encode), encode))) { String s = encode; return s; } } catch (Exception exception) { } encode = "ISO-8859-1"; try { if (str.equals(new String(str.getBytes(encode), encode))) { String s1 = encode; return s1; } } catch (Exception exception1) { } encode = "UTF-8"; try { if (str.equals(new String(str.getBytes(encode), encode))) { String s2 = encode; return s2; } } catch (Exception exception2) { } encode = "GBK"; try { if (str.equals(new String(str.getBytes(encode), encode))) { String s3 = encode; return s3; } } catch (Exception exception3) { } return ""; } /** * 比較兩個日期是否相等 */ public static boolean compareDate(Date date1,Date date2){ if((date1 == null && date2 == null) || (date1!=null && date2!=null && date1.compareTo(date2) == 0)){ return true; }else{ return false; } } /** * 拆分集合 * @param <T> * @param resList 要拆分的集合 * @param count 每一個集合的元素個數 * @return 返回拆分後的各個集合 */ public static <T> List<List<T>> splitArray(List<T> resList,int count){ if(resList==null ||count<1) return null ; List<List<T>> ret=new ArrayList<List<T>>(); int size=resList.size(); if(size<=count){ //數據量不足count指定的大小 ret.add(resList); }else{ int pre=size/count; int last=size%count; //前面pre個集合,每一個大小都是count個元素 for(int i=0;i<pre;i++){ List<T> itemList=new ArrayList<T>(); for(int j=0;j<count;j++){ itemList.add(resList.get(i*count+j)); } ret.add(itemList); } //last的進行處理 if(last>0){ List<T> itemList=new ArrayList<T>(); for(int i=0;i<last;i++){ itemList.add(resList.get(pre*count+i)); } ret.add(itemList); } } return ret; } //匹配正則表達式(只能是字母或數字) public static boolean matches(String content){ String regex = "^[A-Za-z0-9]+$"; Pattern pattern = Pattern.compile(regex); Matcher match = pattern.matcher(content); if(match.matches()){ return true; }else{ return false; } } //匹配正則表達式 public static boolean matches(String regex,String content){ Pattern pattern = Pattern.compile(regex); Matcher match = pattern.matcher(content); if(match.matches()){ return true; }else{ return false; } } }
支持非空校驗、經常使用日期格式轉換、String字符串Array數組List集合互轉,支持分割符內容自定義、表達式匹等;工具
UtilsTools工具類使用(帶註釋):優化