首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

数据印证

2012-11-10 
数据验证import java.util.Dateimport java.util.Listpublic class ValidateUtil {public static final

数据验证
import java.util.Date;

import java.util.List;

public class ValidateUtil {

public static final String DATE_FORMAT_1 = "yyyy/MM/dd";
public static final String DATE_FORMAT_2 = "MM/dd/yyyy";
public static final String DATE_FORMAT_3 = "dd/MM/yyyy";
public static final String DATE_FORMAT_4 = "yyyy/MM/dd hh:mm:ss";
public static final String DATE_FORMAT_5 = "MM/dd/yyyy hh:mm:ss";
public static final String DATE_FORMAT_6 = "dd/MM/yyyy hh:mm:ss";
public static final String TIME_FORMAT_1 = "hh:mm:ss";
public static final String TIME_FORMAT_2 = "hh:mm";

public static boolean isEmail(String email) {
if (email == null || email.length() < 5)
return false;

String regex = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
return email.matches(regex);
}

public static boolean isEmpty(String val) {

return val == null || val.trim().length() == 0;
}

public static boolean isEqual(String val1,String val2) {
if(val1 != null){
return val1.equals(val2);
}else if(val2 != null){
return val2.equals(val1);
}else{
return true;
}
}

/**
* Rule: length: >= 8 <= 20 characters, at least one upper case character, and one digit.
*/
public static boolean checkPassword(String password) {
if (password == null || password.length() < 8 || password.length() > 20)
return false;

char [] pwd = password.trim().toCharArray();
        int c = 0;
        for (int i = 0; i < pwd.length; i++) {
            if ((int)pwd[i] >= 48 && (int)pwd[i] <= 57) {
                c++;
                break;
            }
        }
        if(c==0){
            return false;
        }
        c = 0;
        for (int i = 0; i < pwd.length; i++)
        {
            if ((int)pwd[i] >= 65 && (int)pwd[i] <= 90)
            {
                c++;
                break;
            }
        }
        if (c == 0)
        {
            return false;
        }
       
        return true;
}

public static boolean checkPassword(String password,String regex) {
if (password == null || password.length() <= 0)
return false;

return password.matches(regex);
}

public static boolean isAtoZ(String val) {

if (val == null || val.length() <= 0)
return false;

String regex = "^[A-Za-z]+$";
return val.matches(regex);
}

public static boolean isNumber(String val) {
if (val == null || val.length() <= 0)
return false;

String regex = "^-?([1-9]\\d*\\.?\\d*|0\\.?\\d*[1-9]\\d*|0?\\.?0+|0)$";
return val.matches(regex);

}

public static boolean isInteger(String val) { 

if (val == null || val.length() <= 0)
return false;

String regex = "^-?[1-9]\\d*$";
return val.matches(regex);
}

public static boolean isPositiveInteger(String val) {

if (val == null || val.length() <= 0)
return false;

String regex = "^[1-9]\\d*$";
return val.matches(regex);
}

public static boolean isDate(String date) {
if (date == null)
return false;

String regex = "^[0-9]{4}/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8])))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29)$";
if(date.matches(regex)){
return true;
}

regex = "^((((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|[2][0-8])))/[0-9]{4})|(02/29/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))$";
if(date.matches(regex)){
return true;
}

regex = "^((((0[1-9]|[12][0-9]|3[01]))/(0[13578]|1[02])|((0[1-9]|[12][0-9]|30)/(0[469]|11))|((0[1-9]|[1][0-9]|[2][0-8]))/02)/[0-9]{4})|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))$";
if(date.matches(regex)){
return true;
}

regex = "([0-9]{4}/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8])))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29)) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
if(date.matches(regex)){
return true;
}

regex = "^(((((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|[2][0-8])))/[0-9]{4})|(02/29/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
if(date.matches(regex)){
return true;
}

regex = "^(((((0[1-9]|[12][0-9]|3[01]))/(0[13578]|1[02])|((0[1-9]|[12][0-9]|30)/(0[469]|11))|((0[1-9]|[1][0-9]|[2][0-8]))/02)/[0-9]{4})|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
if(date.matches(regex)){
return true;
}

regex = "^([0-1][0-9]|2[0-3]):([0-5][0-9])$";
if(date.matches(regex)){
return true;
}

regex = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
if(date.matches(regex)){
return true;
}

return false;
}

public static boolean isDate(String date, String format) {
String regex = "";
if(DATE_FORMAT_1.equals(format)){
regex = "^[0-9]{4}/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8])))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29)$";
}else if(DATE_FORMAT_2.equals(format)){
regex = "^((((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|[2][0-8])))/[0-9]{4})|(02/29/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))$";
}else if(DATE_FORMAT_3.equals(format)){
regex = "^((((0[1-9]|[12][0-9]|3[01]))/(0[13578]|1[02])|((0[1-9]|[12][0-9]|30)/(0[469]|11))|((0[1-9]|[1][0-9]|[2][0-8]))/02)/[0-9]{4})|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))$";

}else if(DATE_FORMAT_4.equals(format)){
regex = "([0-9]{4}/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8])))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29)) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
}else if(DATE_FORMAT_5.equals(format)){
regex = "^(((((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|[2][0-8])))/[0-9]{4})|(02/29/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
}else if(DATE_FORMAT_6.equals(format)){
regex = "^(((((0[1-9]|[12][0-9]|3[01]))/(0[13578]|1[02])|((0[1-9]|[12][0-9]|30)/(0[469]|11))|((0[1-9]|[1][0-9]|[2][0-8]))/02)/[0-9]{4})|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";

}else if(TIME_FORMAT_1.equals(format)){
regex = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
}else if(TIME_FORMAT_2.equals(format)){
regex = "^([0-1][0-9]|2[0-3]):([0-5][0-9])$";
}else{
return false;
}

return date.matches(regex);
}

public static int compareDate(Date date1, Date date2) throws NullPointerException{

if(date1 == null || date2 == null)
throw new NullPointerException();

return date1.compareTo(date2);
}

public static boolean minlength(String str, Integer compareVal) {
if (str == null)
return false;

return str.length() >= compareVal;
}

public static boolean maxlength(String str, Integer compareVal) {

if (str == null)
return false;

return str.length() <= compareVal;
}

public static boolean rangeLength(String str, Integer min, Integer max) {
if (str == null)
return false;

return str.length() >= min && str.length() <= max;
}

public static boolean rangeValue(int val, int min, int max) {

return val >= min && val <= max;
}

public static boolean rangeValue(double val, double min, double max) {
return val >= min && val <= max;
}

public static boolean rangeValue(char chars, char min, char max) {

return chars >= min && chars <= max;
}

public static boolean isExist(String val,String [] source) {

if(source == null || source.length == 0)
return false;

for(String str : source){
if(str != null && str.equals(val)){
return true;
}
}

return false;
}

public static boolean isExist(String val,List<String> source) {

if(source == null || source.size() == 0)
return false;

for(String str : source){
if(str != null && str.equals(val)){
return true;
}
}

return false;
}
}

热点排行