java 小方法 判断字符串是否为时间格式,email格式,手机号格式
//=====================判断时间是否正确格式public static boolean checkDate(String date,String format) {
DateFormat df = new SimpleDateFormat(format);
Date d = null;
try{
d = df.parse(date);
}catch(Exception e){
//如果不能转换,肯定是错误格式
return false;
}
String s1 = df.format(d);
// 转换后的日期再转换回String,如果不等,逻辑错误.如format为"yyyy-MM-dd",date为
// "2006-02-31",转换为日期后再转换回字符串为"2006-03-03",说明格式虽然对,但日期
// 逻辑上不对.
return date.equals(s1);
}
//=====================判断时间是否正确格式
//=====================判断邮件email是否正确格式
public boolean checkEmail(String email){
Pattern pattern = Pattern.compile("^/w+([-.]/w+)*@/w+([-]/w+)*/.(/w+([-]/w+)*/.)*[a-z]{2,3}$");
Matcher matcher = pattern.matcher(email);
if (matcher.matches()) {
return true;
}
return false;
}
//=====================判断邮件email是否正确格式
//=====================判断手机号phone是否正确格式
public boolean checkPhone(String phone){
Pattern pattern = Pattern.compile("^13/d{9}||15[8,9]/d{8}$");
Matcher matcher = pattern.matcher(phone);
if (matcher.matches()) {
return true;
}
return false;
}
//=====================判断手机号phone是否正确格式
转载的