private static String channelCode="ABC";
private static SimpleDateFormat ymdFormat=new SimpleDateFormat("yyyyMMdd");
private static SimpleDateFormat hmsFormat=new SimpleDateFormat("HHmmssSSS");
private static final char[] _CHAR_MAGIC_CHAR=new char[]{'0','1','2','3','4','5','6','7','8','9','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'} ;
private static AtomicInteger seed= new AtomicInteger(Integer.MAX_VALUE);
/**
* 格式:标识+YYYYMMDD+交易码+11位流水,保证流水的唯一性
* @return
*/
public static String getSN(String strOpearteCode){
strOpearteCode=strOpearteCode.split("_")[0];
Date curr=new Date();
StringBuffer sb=new StringBuffer(channelCode);//渠道标识
sb.append(ymdFormat.format(curr));//YYYYMMDD
sb.append(strOpearteCode.substring(strOpearteCode.length()-3));//所发交易的“交易码”的最后三位
sb.append(hmsFormat.format(curr));//HHmmssSSS
int seedNum=seed.incrementAndGet();
sb.append(getRundom(seedNum));//两位随机码
return sb.toString();
}
private static String getRundom(int seed){
if(seed>=0){
seed=Integer.MAX_VALUE-seed;
}else{
seed=Integer.MAX_VALUE+1+seed;
}
int mod=seed%(_CHAR_MAGIC_CHAR.length*_CHAR_MAGIC_CHAR.length);
return _CHAR_MAGIC_CHAR[mod/ _CHAR_MAGIC_CHAR.length]+""+_CHAR_MAGIC_CHAR[mod% _CHAR_MAGIC_CHAR.length];
}