1.手机号校验(境内11位)
String phontNo="12356772112";
Pattern pattern = Pattern.compile("^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])" + "|(18[0-9])|(19[8,9]))\\d{8}$");
if (!pattern.matcher(para.getPhoneNo()).matches()) {
System.out.plantln("手机号码格式不对");
}
2.日期年月日时分秒(转载自别处)
private static boolean isValidDate(String str) {
boolean convertSuccess = true;
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
// 设置lenient为false.
// 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
format.setLenient(false);
format.parse(str);
} catch (ParseException e) {
// e.printStackTrace();
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
convertSuccess = false;
}
return convertSuccess;
}
3.校验网址
String jumpUrl = "http://www.baidu.com";
if (StringUtils.isNotBlank(jumpUrl)) {
String regex = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
if (!jumpUrl.matches(regex)) {
System.out.plantln("网址格式有误");
}
}
4.16进制颜色
String color="FFFFFF";
if (!StringUtils.isEmpty(color)) {
String pattern = "^[A-Fa-f0-9]{6}$";
if (!color.matches(pattern)) {
System.out.plantln("16进制颜色格式不对");
}
}
5.Excel导入日期格式
例如:java从Excel单元格读取的日期如43052.0,在后台处理的时候又需要将其处理为日期格式,使用如下代码对其进行转换即可:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = HSSFDateUtil.getJavaDate(Double.parseDouble('43052.0'));
String time = sdf.format(date);
最终time的值为:2017-11-13