java algorithm - Get the ID number born by date

Thinking : ID number is 15 or 17 digit number + a digital / x / X composed of regular expression matching is written is not a string of 15 digits 17 digits, or a + digits / x / X consisting of

Writing a regular expression: ^ (\ D {15} | \ D17 [\ DXX]) $

Output format: yyyy-MM-dd

Bit 7 to 14 declared string variables are used to store three yyyy, MM, dd, identification of the date of birth, but the subscript string starting from 0, so we need 6 begins to take place as the index,

In: [6, 10)

Month: [10, 12)

Day: [12, 14)

Finally, return to the format you want

    / ** 
     * province Syndrome regular expression ^ (\ D {15} | \. 17} {D [\ DX]) $ 
     * @param ID provinces No. 
     * @Return     birthday (the MM-YYYY-dd)
      * / 
    public extractYearMonthDayOfIdCard String (String ID) { 
        String year = null ; 
        String month the = null ; 
        String Day = null ;
          // regular whether a match is correct ID number, 15 or 17 digit number + number / X / X- 
        iF (ID. The matches ( "\\ D ^ {15} |. 17} {D \\ [\\ DXX] $" )) { 
            year = id.substring (. 6, 10 ); 
            month The = id.substring (10,12); 
            Day = id.substring (12, 14 ); 
        } the else { 
            System.out.println ( "ID number does not match!" );
             Return  null ; 
        } 
        return year + "-" + month The + "-" + Day ; 
    }

 

 

Guess you like

Origin www.cnblogs.com/jjking/p/11457219.html