java正则校验,手机号,邮箱,日期格式,时间格式,数字金额两位小数
3.58是否为金额:true
3.582是否为金额:false
1284789@qq.com是否为email:true
1284789qq.com是否为email:false
1823753112是否为手机号:false
18237531125是否为手机号:true
20240127判断字符串是否为日期格式:true
2024-01-27判断字符串是否为日期格式:true
2024/01/27判断字符串是否为日期格式:true
2024/01-27判断字符串是否为日期格式:true
2024-01-27 23:23:23判断字符串是否为日期格式:true
2024-01-27 23:23判断字符串是否为日期格式:false
2024-1-7判断字符串是否为日期格式:true
23:23:23判断字符串是否为时间格式:true
部分代码:源代码请移步文章附件
public class PatternUtils {/*** @title 判断字符串是否是金额(小数点前后是数字即可,无小数点后数据也ok,小数最多2位)* @param amount* @return boolean [-\+]?[.\d]**/private static Pattern amountPattern = Pattern.compile("^(\\-|\\+)?\\d+(\\.\\d{0,2})?$");private static Pattern emailPattern = Pattern.compile("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}");private static Pattern phonePattern = Pattern.compile("0?(13|14|15|18|17)[0-9]{9}");public static boolean strIsAmount(String amount) {if(StringUtils.isEmpty(amount)) {return false;}return amountPattern.matcher(amount).matches();}public static boolean isEmail(String email) {if(StringUtils.isEmpty(email)) {return false;}return emailPattern.matcher(email).matches();}public static boolean isPhone(String phone) {if(StringUtils.isEmpty(phone)) {return false;}return phonePattern.matcher(phone).matches();}/*** 金额验证 - 是否为负数,小数最多两位** @param str* @return boolean*/public static boolean isNegativeNumber(String str) {try {if (StringUtils.isNotEmpty(str)) {String source = str.toString();Pattern pattern = Pattern.compile("-[0-9]+(.[0-9]{0,2}+)?");if (pattern.matcher(source).matches()) {return true;} else {return false;}} else {return false;}} catch (Exception e) {e.printStackTrace();return false;}}/*** 金额验证 - 是否为正数,小数最多两位** @param str* @return boolean*/public static boolean isPositiveNumber(String str) {try {if (StringUtils.isNotEmpty(str)) {String source = str.toString();Pattern pattern = Pattern.compile("^[+]?([0-9]+(.[0-9]{0,2})?)$");if (pattern.matcher(source).matches()) {return true;} else {return false;}} else {return false;}} catch (Exception e) {e.printStackTrace();return false;}}/*** @title 功能:判断字符串是否为日期格式* @param strDate* @return boolean*/public static boolean strIsDateTime(String strDate) {Pattern pattern = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");Matcher m = pattern.matcher(strDate);if (m.matches()) {return true;} else {return false;}}/*** @title 功能:判断字符串是否为时间格式* @param strTime* @return boolean*/public static boolean strIsTime(String strTime) {Pattern pattern = Pattern.compile("^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$");Matcher m = pattern.matcher(strTime);return m.matches();}public static void main(String[] args) {String sr1 = "3.58";System.out.println(sr1 +"是否为金额:"+strIsAmount(sr1));String sr2 = "3.582";System.out.println(sr2 +"是否为金额:"+strIsAmount(sr2));String sr3 = "1284789@qq.com";System.out.println(sr3 +"是否为email:"+isEmail(sr3));String sr4 = "1284789qq.com";System.out.println(sr4 +"是否为email:"+isEmail(sr4));String sr5 = "1823753112";System.out.println(sr5 +"是否为手机号:"+isPhone(sr5));String sr6 = "18237531125";System.out.println(sr6 +"是否为手机号:"+isPhone(sr6));String sr7 = "20240127";System.out.println(sr7 +"判断字符串是否为日期格式:"+strIsDateTime(sr7));String sr71 = "2024-01-27";System.out.println(sr71 +"判断字符串是否为日期格式:"+strIsDateTime(sr71));String sr72 = "2024/01/27";System.out.println(sr72 +"判断字符串是否为日期格式:"+strIsDateTime(sr72));String sr73 = "2024/01-27";System.out.println(sr73 +"判断字符串是否为日期格式:"+strIsDateTime(sr73));String sr74 = "2024-01-27 23:23:23";System.out.println(sr74 +"判断字符串是否为日期格式:"+strIsDateTime(sr74));String sr75 = "2024-01-27 23:23";System.out.println(sr75 +"判断字符串是否为日期格式:"+strIsDateTime(sr75));String sr76 = "2024-1-7";System.out.println(sr76 +"判断字符串是否为日期格式:"+strIsDateTime(sr76));String sr8 = "23:23:23";System.out.println(sr8 +"判断字符串是否为时间格式:"+strIsTime(sr8));}
}