1. 程式人生 > >根據身份證計算週歲,並且按9月之前生的加一歲,之後減一歲

根據身份證計算週歲,並且按9月之前生的加一歲,之後減一歲

package com.yuncai.core.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @Author: LIGuanghua
 * @Description:
 * @Data: Created in 17:25 2018/7/24
 * @Modified By:
 */
public class BirthdayUntil {

    private static final String YINER = "(0~1]:嬰兒";
    private static final String YOUER = "(1~3):幼兒";
    private static final String ERTONG = "[3,6):兒童,上幼兒園";
    private static final String XIAOXUE = "(7~12):少年,上小學";
    private static final String CHUZHONG = "(13~15):少年,上初中";
    private static final String GAOZHONG = "(16~18):少年,上高中";
    private static final String QINGNIAN = "(19~45):青年";
    private static final String ZHONGNIAN = "(46~59):中年";
    private static final String LAONIAN = "(60~89):老年";
    public static void main(String[] args) {

        String s="";
        String birth="1981-04-04";
        s = birth.substring(0, 4);
        String month = birth.substring(5, 7);
        System.out.println(month);
        int result = Integer.parseInt(month);
        System.out.println(result);
        SimpleDateFormat df = new SimpleDateFormat("yyyy");
        String year=df.format(new java.util.Date());
        // int u=Integer.parseInt(year)-Integer.parseInt(s);
        int u=getAgeId("431124199910065712");
        System.out.println(getAge(u,result));
        // new BuildingTimer().addbuildingInfo1();
    }

    public static int getAgeId(String crldNumber){
        Calendar ca =Calendar.getInstance();
        int nowYear= ca.get(Calendar.YEAR);
        int nowMonth= ca.get(Calendar.MONTH)+1;
        int len=crldNumber.length();
        if(len==18){
            int IDYear=Integer.parseInt(crldNumber.substring(6,10));
            int IDMonth=Integer.parseInt(crldNumber.substring(10,12));
            if((IDMonth-nowMonth)>0){
                return nowYear-IDYear-1;
            }
            else
                return nowYear-IDYear;
        }
        else
            System.out.println("錯誤的身份證號");
        return 0;
    }
    public static String getAge(int u,int month){
        String ageName="";
        if (u>=18&&u<=46){
            //青年
            if (u>19&&u<45){
                ageName=QINGNIAN;
            }else{
                if (u==18||u==19){
                    if (month>=9){
                        ageName=GAOZHONG;
                    }else{
                        ageName=QINGNIAN;
                    }
                }else{
                    if (month>=9){
                        ageName= QINGNIAN;
                    }else{
                        ageName=ZHONGNIAN;
                    }
                }
            }
        }else if (u>46&&u<=60){
            //中年
            if (u<59){
                ageName=ZHONGNIAN;
            }else{
                if (month>=9){
                    ageName= ZHONGNIAN;
                }else{
                    ageName=LAONIAN;
                }
            }
        }else if (u>60){
            //老年人
            ageName=LAONIAN;
        }else  if (u>=6&&u<=13){
            //小學
            if (u>7&&u<12){
                ageName=XIAOXUE;
            }else{
                if (u==6||u==7){
                    if (month>=9){
                        ageName=ERTONG;
                    }else{
                        ageName=XIAOXUE;
                    }
                }else{
                    if (month>=9){
                        ageName= XIAOXUE;
                    }else{
                        ageName=CHUZHONG;
                    }
                }
            }
            ageName="少年,上初中";
        }else  if (u==17){
            //高中
            ageName=GAOZHONG;
        }else  if (u>=2&&u<6){
            //兒童幼兒園
            if (u>3){
                ageName=ERTONG;
            }else{
                if (month>=9){
                    ageName= YOUER;
                }else{
                    ageName=ERTONG;
                }
            }
        }else  if (u>13&&u<=16){
            //初中
            if (u>14){
                if (month>=9){
                    ageName= CHUZHONG;
                }else{
                    ageName=GAOZHONG;
                }
            }else{
                ageName=CHUZHONG;

            }
        }else  if (u==1){
            if (month>=9){
                ageName=YINER;
            }else{
                ageName=YOUER;
            }
        }else  if (u<1){
            if (month>=9){
                ageName=YINER;
            }else{
                ageName=YOUER;
            }

        }
        return ageName;
    }
}