订单单号编码动态生成

需求:动态生成订单编号(生成规则:前缀+日期时间+4位随机数)

/**
 * 单号编码
 * @param prefix    传入前缀 如Constant.GD
 * @return
 */
public static String getCode(String prefix){
	String code = getRandomString(4);
	String time = DateUtils.doFormatDate(new Date(), "yyyyMMddHHmmssSSS");
	code = prefix + time + code;
	return code;
}

public static String getRandomString(int length) {
	String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	Random random = new Random();
	StringBuffer sb = new StringBuffer();
	for (int i = 0; i < length; i++) {
		int number = random.nextInt(62);
		sb.append(str.charAt(number));
	}
	return sb.toString().toUpperCase();
}
例:DH 20170721170914253ZFEA

你可能感兴趣的:(Java)