一、正则
1.1只能输入英文和中文
public boolean isEngAndCn(String str){
boolean result = false;
String regEx="^[A-z\\u4e00-\\u9fa5]*$";
Pattern p=Pattern.compile(regEx);
Matcher m=p.matcher(str);
if(!m.matches()) {
result = false;
}else {
result = true;
}
return result;
}
1.2只能输入数字和英文
public boolean isEngAndNum(String str){
boolean result = false;
String regEx="^[A-z0-9]*$";
Pattern p=Pattern.compile(regEx);
Matcher m=p.matcher(str);
if(!m.matches()) {
result = false;
}else {
result = true;
}
return result;
}
二、日期时间
2.1判断是否是合法的日期类型
public static boolean isValidDate(String str) {
//String str = "2007-01-02";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try{
Date date = (Date)formatter.parse(str);
return str.equals(formatter.format(date));
}catch(Exception e){
return false;
}
}
2.2将String类型日期,转成Date
Date date = getDateByString("2013-01-02","yyyy-mm-dd");
System.out.println(date);
public Date getDateByString(String str,String formatStr){
SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
Date paperPeriod = null;
try {
paperPeriod = (Date)dateFormat.parse(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}