package zmo.technology.lite.core;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ZeroIDCard {

    public static boolean checkISO70641983MOD112(String idCard) {
        if (idCard.length() != 18)
            return false;
        char InNum[] = idCard.toCharArray();
        char r = InNum[17];
        int Sum = 0;
        for (int i = 0; i < 17; i++)
            InNum[i] -= 0x30;
        char CheckNum[] = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
        Sum = InNum[0] * 7 + InNum[1] * 9 + InNum[2] * 10 + InNum[3] * 5 + InNum[4] * 8 + InNum[5] * 4 + InNum[6] * 2
                + InNum[7] * 1 + InNum[8] * 6
                + InNum[9] * 3 + InNum[10] * 7 + InNum[11] * 9 + InNum[12] * 10 + InNum[13] * 5 + InNum[14] * 8
                + InNum[15] * 4 + InNum[16] * 2;
        return r == CheckNum[Sum % 11];
    }

    public static Date birthday(String idCard) throws Exception {
        if (idCard.length() != 18)
            throw new ZeroException("error idCard length (" + idCard.length() + "）");
        return new SimpleDateFormat("yyyyMMdd-HH:mm:ss").parse(idCard.substring(6, 14) + "-00:00:00");
    }

    public static int sex(String idCard) throws ZeroException {
        if (idCard.length() != 18)
            throw new ZeroException("error idCard length (" + idCard.length() + "）");
        return Integer.parseInt(idCard.substring(16, 17)) % 2;
    }
}
