/** * * @param date * 時間 * @param format * 格式 * @return */ public static String formatDate(Date date, String format) { return (new SimpleDateFormat(format)).format(date); } public static String formatDate(Date date) { return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date); } public static String formatDateShort(Date date) { return (new SimpleDateFormat("yyyy-MM-dd")).format(date); } /** * 獲得默認的時間字符串 * * @return */ public static String findDefaultDateStr() { return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()); } /** * 根據(jù)時間模板返回字符串 * * @param fmt * @return */ public static String findDateStr(String fmt) { return (new SimpleDateFormat(fmt)).format(new Date()); } /** * 驗證郵箱是否正確 * * @param searchPhrase * @return */ public static boolean isEmail(final String searchPhrase) { if (!isEmpty(searchPhrase)) { final String check = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"; final Pattern regex = Pattern.compile(check); final Matcher matcher = regex.matcher(searchPhrase); return matcher.matches(); } return false; } /** * 驗證手機號是否正確 * * @param mobiles * @return */ public static boolean isMobileNO(String mobiles) { if (!isEmpty(mobiles)) { Pattern p = Pattern .compile("^((13[0-9])|(15[^4,\\D])|(18[0-9])|(14[0-9]))\\d{8}$"); Matcher m = p.matcher(mobiles); return m.matches(); } return false; } /** * 計算字符串長度,,拼接報文頭,不足前補0 * * @param content * @param lenght * @return */ public static String computeLengthMergerStr(String content, int lenght) { return StringUtil.fillNumWithLeft(StringUtil.getStrLength((content)) + "", lenght) + content; } /** * 根據(jù)字節(jié)長度截取字符串 * * @param str * @param begin * @param count * @return * @throws UnsupportedEncodingException */ public static String subBytes(String str, int begin, int count) throws UnsupportedEncodingException { byte[] src = str.getBytes("GBK"); byte[] bs = new byte[count]; for (int i = begin; i < begin + count; i++) { bs[i - begin] = src[i]; } String s = new String(bs, "GBK"); return trim(s); } /** * 獲得六位號隨機數(shù) * * @return */ public static String GetRandomNumberStr6() { Random r = new Random(); // return "888888"; return "" + (r.nextInt(900000) + 100000); } /** * 2元到十元之間隨機數(shù) * * @return */ public static String GetRandomNumber2To10() { long i = Math.round(Math.random() * (799) + 200); // return "888888"; return "" + ArithmeticUtil.divStr(Double.parseDouble("" + i), 100); } public static String GetRandom10To15() { long i = Math.round(Math.random() * (5) + 10); return "" + i; } public static String GetRandom5000To15000() { long i = Math.round(Math.random() * (10) + 5); return "" + i * 1000; } // 體驗金抽獎金額 public static String GetRandomVirtual() { // Random r = new Random(); // int rand = (r.nextInt(100) + 1); // int money = 2000; // if (rand == 2) { // money = 3000; // } // if (rand > 2 && rand <= 5) { // money = 5000; // } // if (rand > 5 && rand <= 10) { // money = 6000; // } // if (rand > 10 && rand <= 20) { // money = 7000; // } // if (rand > 20 && rand <= 30) { // money = 8000; // } // if (rand > 30 && rand <= 40) { // money = 9000; // } // if (rand > 40 && rand <= 100) { // money = 10000; // } // return money + ""; Random r = new Random(); int rand = (r.nextInt(100) + 1); int money = 2000; if (rand == 2) { money = 3000; } if (rand > 2 && rand <= 5) { money = 5000; } if (rand > 5 && rand <= 20) { money = 6000; } if (rand > 20 && rand <= 40) { money = 7000; } if (rand > 40 && rand <= 60) { money = 8000; } if (rand > 60 && rand <= 80) { money = 9000; } if (rand > 80 && rand <= 100) { money = 10000; } return money + ""; } /** * B-F級抽取比例對應(yīng)表: * * @return */ public static String GetRandomB_F() { Random r = new Random(); int rand = (r.nextInt(100) + 1); String fundType = "B"; if (rand > 10 && rand <= 50) { fundType = "C"; } if (rand > 50 && rand <= 85) { fundType = "D"; } if (rand > 85 && rand <= 95) { fundType = "E"; } if (rand > 95 && rand <= 100) { fundType = "F"; } return fundType; } public static String GetRandomNumberStr4() { Random r = new Random(); return "" + (r.nextInt(9000) + 1000); } public static boolean isInteger(String arg) { return (!isEmpty(arg)) && arg.matches("-?[0-9]*"); // 正負數(shù) } public static boolean isPositiveInteger(String arg) { return (!isEmpty(arg)) && arg.matches("[0-9]*"); // 正負數(shù) } /** * 判斷是否為金額,最多兩位小數(shù),,且不能為負 * * @param mobiles * @return */ public static boolean isMoney(String money) { if (!isEmpty(money)) { Pattern p = Pattern.compile("^[0-9]+(\\.[0-9]{1,2})?$"); Matcher m = p.matcher(money); return m.matches(); } return false; } /** * 判斷兩位小數(shù)金額 * * @param money * @return */ public static boolean isMoney(double money) { Pattern p = Pattern.compile("^[0-9]+(\\.[0-9]{1,2})?$"); Matcher m = p.matcher("" + money); return m.matches(); } /** * 判斷兩位小數(shù)金額 * * @param money * @return */ public static boolean isFloatMoney(float money) { Pattern p = Pattern.compile("^[0-9]+(\\.[0-9]{1,2})?$"); Matcher m = p.matcher("" + money); return m.matches(); } /** * 判斷是否為數(shù)字 * * @param str * @return */ public static boolean isNumber(String str) { if (!isEmpty(str)) { return str .matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$"); } return false; } /** * 返回double值,默認為0 * * @param str * @return */ public static double getDefaultDoubleValue(String str) { if (StringUtil.isNumber(str)) { return Double.parseDouble(str); } return 0; } /** * 返回兩數(shù)相除,默認返回0 * * @param str * 除數(shù) * @param d * 被除數(shù) * @return */ public static String getDivDoubleValue(String str, double d) { if (StringUtil.isNumber(str)) { return ArithmeticUtil.divStr(Double.parseDouble(str), d); } return "0"; } /** * 獲得隨機大小英文字符串 * * @param length * @return */ public static final String getRandomString(int length) { if (length < 1) { return null; } if (randGen == null) { randGen = new Random(); } if (numbersAndLetters == null) { numbersAndLetters = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") .toCharArray(); } char[] randBuffer = new char[length]; for (int i = 0; i < randBuffer.length; i++) { randBuffer[i] = numbersAndLetters[randGen.nextInt(51)]; } return new String(randBuffer); } /** * sql攻擊過濾 * * @param str * @return */ public static boolean sqlValidate(String str) { str = str.toLowerCase();// 統(tǒng)一轉(zhuǎn)為小寫 String badStr = "'|and|exec|execute|insert|create|drop|table|from|grant|use|group_concat|column_name|" + "information_schema.columns|table_schema|union|where|select|delete|update|order|by|count|*|" + "chr|mid|master|truncate|char|declare|or|;|-|--|+|,|like|//|/|%|#";// 過濾掉的sql關(guān)鍵字,,可以手動添加 String[] badStrs = badStr.split("\\|"); for (int i = 0; i < badStrs.length; i++) { if (str.indexOf(badStrs[i]) != -1) { return true; } } return false; }
|
|
來自: dabinglibrary > 《java》