java方法封装_Java常用方法封装

/**

*

* @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());

}

/**

* 根据时间模板返回字符串

*

* @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;

}

/**

* 根据字节长度截取字符串

*

* @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);

}

/**

* 获得六位号随机数

*

* @return

*/

public static String GetRandomNumberStr6() {

Random r = new Random();

// return "888888";

return "" + (r.nextInt(900000) + 100000);

}

/**

* 2元到十元之间随机数

*

* @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级抽取比例对应表:

*

* @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]*"); // 正负数

}

public static boolean isPositiveInteger(String arg) {

return (!isEmpty(arg)) && arg.matches("[0-9]*"); // 正负数

}

/**

* 判断是否为金额,最多两位小数,且不能为负

*

* @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;

}

/**

* 判断两位小数金额

*

* @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();

}

/**

* 判断两位小数金额

*

* @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();

}

/**

* 判断是否为数字

*

* @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;

}

/**

* 返回两数相除,默认返回0

*

* @param str

*            除数

* @param d

*            被除数

* @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();// 统一转为小写

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关键字,可以手动添加

String[] badStrs = badStr.split("\\|");

for (int i = 0; i < badStrs.length; i++) {

if (str.indexOf(badStrs[i]) != -1) {

return true;

}

}

return false;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值