Judgment.java 5.85 KB
package com.hjx.personalcenter.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Judgment {
    private static Judgment instance;

    public static Judgment getInstance() {
        if (instance == null) {
            instance = new Judgment();
        }
        return instance;
    }


    // 是英文
    public boolean isEnglish(String english) {
        Pattern pattern = Pattern.compile("^[A-Za-z]+$");
        Matcher m = pattern.matcher(english);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isEnglishAndChinese(String str) {
        int j = 0;
        int k = 0;
        int i = str.length();
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
        Matcher m = pattern.matcher(str);
        while (m.find()) {
            j++;
        }
        for (int idx = 0; idx < i; idx++) {
            char c = str.charAt(idx);
            int tmp = (int) c;
            if ((tmp >= 'a' && tmp <= 'z') || (tmp >= 'A' && tmp <= 'Z') || tmp == '.') {
                k++;
            }
        }
        if (i == j + k) {
            return true;
        } else {
            return false;
        }

    }

    //英文和数字
    public boolean isEnglishAndDigit(String str) {
        int j = 0;
        int k = 0;
        int i = str.length();
        for (int idx = 0; idx < str.length(); idx++) {
            if (Character.isDigit(str.charAt(idx))) {
                j++;
            }
        }
        for (int idx = 0; idx < i; idx++) {
            char c = str.charAt(idx);
            if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') {
                k++;
            }
        }
        if ((k + j) == i) {
            return false;
        }

        return true;
    }

    public boolean isChinese(String name) {
        int j = 0;
        int i = name.length();
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
        Matcher m = pattern.matcher(name);
        while (m.find()) {
            j++;
        }
        if (i == j) {
            return false;
        } else {
            return true;
        }
    }

    //中文和点
    public boolean isChineseandPoint(String name) {
        int j = 0;
        int i = name.length();
        int k = 0;
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
        Matcher m = pattern.matcher(name);
        while (m.find()) {
            j++;
        }
        for (int idx = 0; idx < i; idx++) {
            char c = name.charAt(idx);
            if (c == '.') {
                k++;
            }
        }
        if (i == j + k) {
            return true;
        } else {
            return false;
        }

    }

    //数字和*
    public boolean isJustDigitStar(String str) {

        int len = 0;
        for (int idx = 0; idx < str.length(); idx++) {
            if (Character.isDigit(str.charAt(idx)) || str.charAt(idx) == '*') {
                len++;
            }
        }

        if (len == str.length()) {
            return true;
        }

        return false;
    }

    //	所有都为数字
    public boolean isAllDigit(String str) {
        int len = 0;
        for (int idx = 0; idx < str.length(); idx++) {
            if (Character.isDigit(str.charAt(idx))) {
                len++;
            }
        }
        if (len == str.length()) {
            return true;
        }

        return false;
    }

    public boolean isEnglishAndBackSpace(String str) {
        int len = 0;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == ' ') {
                len++;
            }
        }
        if (len == str.length()) {
            return true;
        }
        return false;
    }

    // 地址
    public boolean isAddress(String address) {
        int i = 0, j = 0, k = 0;
        int count = address.length();
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
        Matcher m = pattern.matcher(address);
        while (m.find()) {
            i++;
        }
        for (int idx = 0; idx < count; idx++) {
            char c = address.charAt(idx);
            int tmp = (int) c;
            if ((tmp >= 'a' && tmp <= 'z') || (tmp >= 'A' && tmp <= 'Z')) {
                j++;
            }

            if (Character.isDigit(address.charAt(idx))) {
                k++;
            }

        }
        if ((i + j + k) == count) {
            return true;
        } else {
            return false;
        }

    }

    public boolean judgeContainsIAndO(String str) {
        for (int i = 1; i < str.length(); i++) {
            if (str.charAt(i) == 'I' || str.charAt(i) == 'O') {
                return false;
            }
        }
        return true;
    }


    //手机号码
    public boolean isPhoneNum(String phonenum) {

        if (phonenum.length() != 11 || !phonenum.substring(0, 1).equals("1")) {
            return true;
        } else {
            return false;
        }
    }


    public boolean valibrithday(String beginTime, String brithday) {
        if (brithday != null) {
            String brith[] = brithday.split("-");
            int year = Integer.parseInt(brith[0]);
            int month = Integer.parseInt(brith[1]);
            int day = Integer.parseInt(brith[2]);
            String strBegin[] = beginTime.split("-");

            int beginYear = Integer.parseInt(strBegin[0]);
            int beginMonth = Integer.parseInt(strBegin[1]);
            int beginDay = Integer.parseInt(strBegin[2]);
            // 杈撳叆鐨勫紑濮嬫棩鏈熶笉鑳芥棭浜庡嚭鐢熸棩璧�
            if ((beginYear * 365 + beginMonth * 30 + beginDay) > (year * 365
                    + month * 30 + day)) {
                return true;
            } else {
                return false;
            }

        } else {
            return false;
        }
    }
}