java工具--随机身份证、生日、下标、获取身份证性别

package com.zhongma.people.car.fitting.tool;

import java.util.Random;

/**
 * @author konglcwqy
 * @description
 * @date 2023/8/11 14:36
 */
public class RandomTool {

    private static String[] YEAR = new String[]{
            "1979", "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989",
            "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000",
            "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011",
            "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022"
    };
    private static String[] MONTH = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
    private static String[] DATE = new String[]{
            "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14",
            "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28"

    };
    private static String[] NUM_WORD = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "X"};
    private static String[] MONTH_ARR = new String[]{"2019", "2020", "2021", "2022", "2023"};

    /**
     * 功能描述:随机产生一个下标
     *
     * @param
     * @return int
     * @author konglcwqy
     * @date 2023/8/11 14:35
     */
    public static int randomIndex(int bound) {
        Random random = new Random();
        return random.nextInt(bound);
    }

    /**
     * 功能描述:随机生日
     *
     * @param
     * @return java.lang.String
     * @author konglcwqy
     * @date 2023/8/11 14:51
     */
    public static String randomBirthday() {
        StringBuilder sb = new StringBuilder();
        int index = randomIndex(YEAR.length);
        sb.append(YEAR[index]);
        index = randomIndex(MONTH.length);
        sb.append(MONTH[index]);
        index = randomIndex(DATE.length);
        sb.append(DATE[index]);
        return sb.toString();
    }

    /**
     * 功能描述:随机生日
     *
     * @param
     * @return java.lang.String
     * @author konglcwqy
     * @date 2023/8/11 14:51
     */
    public static Integer randomMonth() {
        StringBuilder sb = new StringBuilder();
        int index = randomIndex(MONTH_ARR.length);
        sb.append(MONTH_ARR[index]);
        index = randomIndex(MONTH.length);
        sb.append(MONTH[index]);
        return Integer.parseInt(sb.toString());
    }

    /**
     * 功能描述:随机身份证号
     *
     * @param area 前四位:省市
     * @return java.lang.String
     * @author konglcwqy
     * @date 2023/8/11 14:45
     */
    public static String randomIdCardNum(String[] area) {
        int index = randomIndex(area.length);
        StringBuilder sb = new StringBuilder();
        //4
        sb.append(area[index]);
        //产生区
        index = randomIndex(NUM_WORD.length - 1);
        sb.append(NUM_WORD[index]);
        index = randomIndex(NUM_WORD.length - 1);
        sb.append(NUM_WORD[index]);
        //产生生日
        sb.append(randomBirthday());
        //产生生日后两位
        index = randomIndex(NUM_WORD.length - 1);
        sb.append(NUM_WORD[index]);
        index = randomIndex(NUM_WORD.length - 1);
        sb.append(NUM_WORD[index]);
        //产生性别
        index = randomIndex(2);
        sb.append(NUM_WORD[index]);
        //产生最后一位
        index = randomIndex(NUM_WORD.length);
        sb.append(NUM_WORD[index]);
        return sb.toString();
    }

    /**
     * 功能描述:获取性别
     *
     * @param idCardNum
     * @return java.lang.String
     * @author konglcwqy
     * @date 2023/8/11 15:13
     */
    public static String getSex(String idCardNum) {
        String sex = idCardNum.substring(16, 17);
        int i = Integer.parseInt(sex);
        if (i % 2 == 0) {
            return "2";
        }
        return "1";
    }
}

你可能感兴趣的:(java,开发语言)