身份证转生日 15位 ,18位

/**
 * 身份证转生日
 * @param IDcard
 * @return
 */
public  static String convertIDcardToBirthday( String  IDcard){
    String birthday = null ;
    if(IDcard.length() ==15){
        String b = IDcard.substring(6,12);
        String bir = "19"+b;
        LocalDate birth  = LocalDate.from(DateTimeFormatter.ofPattern("yyyyMMdd").parse(bir));
        birthday =  birth.format( DateTimeFormatter.ofPattern("yyyy-MM-dd")  );
    }else  if (IDcard.length()==18){
        String bir = IDcard.substring(6,14);
        LocalDate birth  = LocalDate.from(DateTimeFormatter.ofPattern("yyyyMMdd").parse(bir));
        birthday =  birth.format( DateTimeFormatter.ofPattern("yyyy-MM-dd")  );
    } else {
        return "9999-01-01";
    }
    return  birthday ;
}


public static void main(String[] args) {
    System.out.println(convertIDcardToBirthday("110101192003077537"));
    System.out.println(convertIDcardToBirthday("11010120060307453X"));
    System.out.println(convertIDcardToBirthday("632123820927051"));

}

你可能感兴趣的:(Java)