实用小工具

1.出生日期字符串转换为日期

    /**
     * 将 生日 计算为 年龄
     *
     * @param strDate
     * @return Date;
     */
    public static Date parse(String strDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.parse(strDate);
    }

2.通过出生日期计算出年龄

    /**
     * 将 生日 计算为 年龄
     *
     * @param birth
     * @return int;
     */
    public int getAge(Date birth) {
        Calendar cal = Calendar.getInstance();
        int thisYear = cal.get(Calendar.YEAR);
        int thisMonth = cal.get(Calendar.MONTH);
        int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

        cal.setTime(birth);
        int birthYear = cal.get(Calendar.YEAR);
        int birthMonth = cal.get(Calendar.MONTH);
        int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

        int age = thisYear - birthYear;

        // 未足月
        if (thisMonth <= birthMonth) {
            // 当月
            if (thisMonth == birthMonth) {
                // 未足日
                if (dayOfMonth < birthdayOfMonth) {
                    age--;
                }
            } else {
                age--;
            }
        }
        return age;
    }

3.Date类型与LocalDate类型相互转换

    /**
     * 将 Date 转为 LocalDate
     *
     * @param date
     * @return java.time.LocalDate;
     */
    public LocalDate dateToLocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * 将 LocalDate 转为 Date
     *
     * @param localDate
     * @return java.util.Date;
     */
    public Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

4.通过身份证获取生日、年龄、性别方法

    /** 
     * 通过身份证号码获取出生日期(birthday)、年龄(age)、性别(sex) 
     * @param idCardNo 身份证号码 
     * @return 返回的出生日期格式:1993-05-07   性别格式:1:男,0:女 
     */ 
    public static Map<String, String> getBirthdayAgeSex(String idCardNo) { 
        String birthday = ""; 
        String age = ""; 
        String sexCode = ""; 
 
        int year = Calendar.getInstance().get(Calendar.YEAR); 
        char[] number = idCardNo.toCharArray(); 
        boolean flag = true; 
        if (number.length == 15) { 
            for (int x = 0; x < number.length; x++) { 
                if (!flag){ 
                    return new HashMap<String, String>(); 
                } 
                flag = Character.isDigit(number[x]); 
            } 
        } else if (number.length == 18) { 
            for (int x = 0; x < number.length - 1; x++) { 
                if (!flag){ 
                    return new HashMap<String, String>(); 
                } 
                flag = Character.isDigit(number[x]); 
            } 
        } 
        if (flag && idCardNo.length() == 15) { 
            birthday = "19" + idCardNo.substring(6, 8) + "-" 
                    + idCardNo.substring(8, 10) + "-" 
                    + idCardNo.substring(10, 12); 
            sexCode = Integer.parseInt(idCardNo.substring(idCardNo.length() - 3, idCardNo.length())) % 2 == 0 ? "0" : "1"; 
            age = (year - Integer.parseInt("19" + idCardNo.substring(6, 8))) + ""; 
        } else if (flag && idCardNo.length() == 18) { 
            birthday = idCardNo.substring(6, 10) + "-" 
                    + idCardNo.substring(10, 12) + "-" 
                    + idCardNo.substring(12, 14); 
            sexCode = Integer.parseInt(idCardNo.substring(idCardNo.length() - 4, idCardNo.length() - 1)) % 2 == 0 ? "0" : "1"; 
            age = (year - Integer.parseInt(idCardNo.substring(6, 10))) + ""; 
        } 
        Map<String, String> map = new HashMap<String, String>(); 
        map.put("birthday", birthday); 
        map.put("age", age); 
        map.put("sex", sexCode); 
        return map; 
    }

posted @ 2021-11-12 17:40  Whlinde  阅读(71)  评论(0)    收藏  举报