자바 주민등록번호 검사

6277 단어 Java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Main1 {
    /**
     * 15     
     */
    private static final Integer FIFTEEN_ID_CARD = 15;

    /**
     * 18     
     */
    private static final Integer EIGHTEEN_ID_CARD = 18;

    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    /**
     *           
     * @param IDCard
     * @return
     */
    public static String getSex(String IDCard) {
        String sex = "";
        if (IDCard != null) {
            //15     
            if (IDCard.length() == FIFTEEN_ID_CARD) {
                if (Integer.parseInt(IDCard.substring(14, 15)) % 2 == 0) {
                    sex = " ";
                } else {
                    sex = " ";
                }
                //18     
            } else if (IDCard.length() == EIGHTEEN_ID_CARD) {
                //     
                if (Integer.parseInt(IDCard.substring(16).substring(0, 1))
                        % 2 == 0) {
                    sex = " ";
                } else {
                    sex = " ";
                }
            }
        }
        return sex;
    }

    /**
     *           
     * @param IDCard
     * @return
     */
    public static Integer getAge(String IDCard) {
        Integer age = 0;
        Date date = new Date();
        if (IDCard != null && isValid(IDCard)) {
            //15     
            if (IDCard.length() == FIFTEEN_ID_CARD) {
                //        (15     1980   )
                String uyear = "19" + IDCard.substring(6, 8);
                //        
                String uyue = IDCard.substring(8, 10);
                //     
                String fyear = format.format(date).substring(0, 4);
                //     
                String fyue = format.format(date).substring(5, 7);
                if (Integer.parseInt(uyue) <= Integer.parseInt(fyue)) {
                    age = Integer.parseInt(fyear) - Integer.parseInt(uyear) + 1;
                    //         
                } else {
                    age = Integer.parseInt(fyear) - Integer.parseInt(uyear);
                }
                //18     
            } else if (IDCard.length() == EIGHTEEN_ID_CARD) {
                //        
                String year = IDCard.substring(6).substring(0, 4);
                //        
                String yue = IDCard.substring(10).substring(0, 2);
                //     
                String fyear = format.format(date).substring(0, 4);
                //     
                String fyue = format.format(date).substring(5, 7);
                //                    
                if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) {
                    age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
                    //          
                } else {
                    age = Integer.parseInt(fyear) - Integer.parseInt(year);
                }
            }
        }
        return age;
    }

    /**
     *         yyyy MM dd 
     * @param IDCard
     * @return
     */
    public static String getBirthday(String IDCard) {
        String birthday = "";
        String year = "";
        String month = "";
        String day = "";
        if (IDCard != null) {
            //15     
            if (IDCard.length() == FIFTEEN_ID_CARD) {
                //        (15     1980   )
                year = "19" + IDCard.substring(6, 8);
                //       
                month = IDCard.substring(8, 10);
                //       
                day = IDCard.substring(10, 12);
                //18     
            } else if (IDCard.length() == EIGHTEEN_ID_CARD) {
                //        
                year = IDCard.substring(6).substring(0, 4);
                //        
                month = IDCard.substring(10).substring(0, 2);
                //       
                day = IDCard.substring(12).substring(0, 2);
            }
            birthday = year + " " + month + " " + day + " ";
        }
        return birthday;
    }

    /**
     *      
     * @param id     
     * @return     
     */
    public static boolean isValid(String id) {
        Boolean validResult = true;
        //       15 18
        int len = id.length();
        if (len != FIFTEEN_ID_CARD && len != EIGHTEEN_ID_CARD) {
            validResult = false;
        }
        //    
        if (!validDate(id)) {
            validResult = false;
        }
        return validResult;
    }

    /**
     *     
     * @param id
     * @return
     */
    private static boolean validDate(String id) {
        try {
            String birth = id.length() == 15 ? "19" + id.substring(6, 12)
                    : id.substring(6, 14);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Date birthDate = sdf.parse(birth);
            if (!birth.equals(sdf.format(birthDate))) {
                return false;
            }
        } catch (ParseException e) {
            return false;
        }
        return true;
    }

    /**
     * 18           
     * @param cardId
     * @return      true
     */
    public static boolean id18check(String cardId) {
        if (StringUtils.isNotBlank(cardId)) {
            char[] id = cardId.toCharArray();
            int sum = 0;
            int w[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            char[] ch = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3',
                    '2' };
            for (int i = 0; i < id.length - 1; i++) {
                sum += (id[i] - '0') * w[i];
            }
            int c = sum % 11;
            char code = ch[c];
            char last = id[id.length - 1];
            last = last == 'x' ? 'X' : last;
            return last == code;
        }
        return false;
    }

    public static void main(String[] args) {
        boolean a = validDate("218591199713191221");
        System.out.println(a);
    }
}

좋은 웹페이지 즐겨찾기