java 短信订单ID生成类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 短信订单ID生成类相关的知识,希望对你有一定的参考价值。


import java.util.Random;


/**
 * 短信订单id生成类
 */
public class SMSOrderIDGenerator {

	private String[] exception = {"null","nulL","nuLl","nuLL","nUll","nUlL","nULl","nULL","Null","NulL","NuLl","NuLL","NUll","NUlL","NULl","NULL"};
	
	private static char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C',
			'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	private static final int maxNum = 62;

	/**
	 * 得到订单id
	 * @param length 长度
	 * @return
	 */
	public static String getOrderID(int length) {

//		String generatedID = getRandomOrder(12);
		String generatedID = getRandomOrder(length);
		while(generatedID.toLowerCase().contains("null")){
			generatedID = getRandomOrder(length);
		}
		String app_id = DKSingleSDKSettings.SDK_APPID;
		String promotion_id = DKSingleSDKSettings.SDK_CHANNELID;

		// return app_id + promotion_id + generatedID;
		return generatedID;

	}

	/**
	 * 得到随机订单
	 * @param length 长度
	 * @return
	 */
	private static String getRandomOrder(int length) {

		int count = 0;
		int i = 0;

		StringBuffer pwd = new StringBuffer("");
		Random r = new Random();

		while (count < length) {

			i = Math.abs(r.nextInt(maxNum)); // 生成的数最大为62-1

			if (i >= 0 && i < str.length) {
				pwd.append(str[i]);
				count++;
			}
		}

		return pwd.toString();
	}
}

以上是关于java 短信订单ID生成类的主要内容,如果未能解决你的问题,请参考以下文章

java 生成订单号ID的程序

java生成订单编号工具类

thinkphp集成系列之短信验证码订单通知

thinkphp集成系列之短信验证码订单通知

thinkphp整合系列之短信验证码订单通知

java订单生成工具类