package com.gulf.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* 正则判断工具类
*
* @author gulf -08-12
*/
public class RegularUtil {
/**
* 判断是否为数字
*
* @param param
* @return
*/
public static boolean isDigit(String param) {
if (param.matches("[0-9]+")) {
return true;
}
return false;
}
/**
* 判断是否为车牌号码
*
* @param param
* @return
*/
public static boolean isVehiclePlate(String param) {
if ((param.matches("^(([\u4e00-\u9fa5]{1})|([A-Z]{1}))[A-Z]{1}[A-Z0-9]{4}(([\u4e00-\u9fa5]{1})|([A-Z0-9]{1}))$")
|| param.matches("^WJ[0-9]{2}(([\u4e00-\u9fa5]{1})|([0-9]{1})[0-9]{4})$"))
&& param.matches("^([\u4e00-\u9fa5]*[a-zA-Z0-9]+){6,}$") && param.matches("^.{3}((?!.*O)(?!.*I)).*$")) {
return true;
}
return false;
}
/**
* 整数最多十位,小数最多两位
*
* @param param
* @return
*/
public static boolean isNumber12_2(String param) {
if (param.matches("^(([1-9]\\d{0,9})|0)(\\.\\d{1,2})?$")) {
return true;
}
return false;
}
/**
* 整数最多8位,小数最多两位
*
* @param param
* @return
*/
public static boolean isNumber10_2(String param) {
if (param.matches("^(([1-9]\\d{0,7})|0)(\\.\\d{1,2})?$")) {
return true;
}
return false;
}
/**
* 判断是否为手机号
*
* @param param
* @return
*/
public static boolean isMobile(String param) {
if (param.matches("^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8]))\\d{8}$")) {
return true;
}
return false;
}
/**
* 判断是否为邮政编码
*
* @param param
* @return
*/
public static boolean isZipCode(String param) {
if (param.matches("^[0-9]{6}$")) {
return true;
}
return false;
}
/**
* 判断是否为电子邮箱
*
* @param param
* @return
*/
public static boolean isEmail(String param) {
if (param.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*")) {
return true;
}
return false;
}
/**
* 判断是否为车辆识别代码(车架号)
*
* @param param
* @return
*/
public static boolean isIdentificationCode(String param) {
if (param.matches("^[A-Z0-9]{6,17}$")) {
return true;
}
return false;
}
/**
* 判断是否为发动机号
*
* @param param
* @return
*/
public static boolean isVehicleEngineNo(String param) {
if (param.matches("^[A-Z0-9]+$")) {
return true;
}
return false;
}
/**
* 判断是不是金额
*
* @param param
* @return
*/
public static boolean isMoney(String param) {
if (param.matches("^(([1-9]\\d{0,9})|0)(\\.\\d{1,2})?$")) {
return true;
}
return false;
}
/**
* 格式是yyyy/MM/dd HH:mm:ss
* @param param
* @return
* @return boolean
*/
public static boolean isValidDate(String param) {
boolean convertSuccess = true;
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期
try {
format.setLenient(false);
format.parse(param);
} catch (ParseException e) {
convertSuccess = false;
}
return convertSuccess;
}
/**
* 格式是yyyy-MM-dd
* @param param
* @return
* @return boolean
*/
public static boolean isValidDay(String param) {
boolean convertSuccess = true;
// yyyy-MM-dd
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期
try {
format.setLenient(false);
format.parse(param);
} catch (ParseException e) {
convertSuccess = false;
}
return convertSuccess;
}
public static void main(String[] args) {
// test
// System.out.println(RegularUtil.isDigit("1")+"");
// System.out.println(RegularUtil.isVehiclePlate("粤a12345")+"");
// System.out.println(RegularUtil.isNumber12_2("1234567811.11")+"");
// System.out.println(RegularUtil.isZipCode("555555")+"");
// System.out.println(RegularUtil.isEmail("5555551111111111111111111111111111111@")+"");
// System.out.println(RegularUtil.isMobile("13333333333")+"");
// System.out.println(RegularUtil.isIdentificationCode("111111")+"");
// System.out.println(RegularUtil.isVehicleEngineNo("234")+"");
// System.out.println(RegularUtil.isMoney("aaa")+"");
System.out.println(RegularUtil.isNumber10_2("2345089.11")+"");
}
}