数字类型验证:不是数字 或 是负数 返回 true
public static boolean isNotNumOrNegative(Number num) { if (num == null) { return true; } if (num instanceof Integer) { int v = num.intValue(); return v < 0; } if (num instanceof Long) { long v = num.longValue(); return v < 0; } if (num instanceof BigInteger) { BigInteger v = (BigInteger) num; return v.compareTo(BigInteger.valueOf(0L)) < 0; } if (num instanceof BigDecimal) { BigDecimal v = (BigDecimal) num; return v.compareTo(BigDecimal.valueOf(0L)) < 0; } return false; } public static boolean isNotNumOrNegative(Number... nums) { for (Number num : nums) { if (isNotNumOrNegative(num)) { return true; } } return false; }
字符串验证:
public static boolean isNullOrEmpty(String val) { return val == null || "".equals(val.trim()); } public static boolean isNullOrEmpty(String... vals) { for (String val : vals) { if (isNullOrEmpty(val)) { return true; } } return false; }
生成6位随机字符:
public static String getRandomChar() { StringBuilder chars = new StringBuilder(); for (int i = 0; i < 6; i++) { int index = new Random().nextInt(26) + 65; chars.append((char) index); } return chars.toString(); }
生成6位验证码:
String.format("%06d", (int) (Math.random() * 1000000))