1 //用于生成出库的ID 2 public static String getexportid(){ 3 //生成规则:Ck+时间戳(20170626143520)+6位的随机数 4 return "CK"+getnowstr()+getStringRandom(6); 5 } 6 public static String getstorageid(){ 7 //生成规则:Rk+时间戳(20170626143520)+6位的随机数 8 return "RK"+getnowstr()+getStringRandom(8); 9 } 10 11 private static String getnowstr(){ 12 Date date = new Date(); 13 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); 14 String str = sdf.format(date); 15 return str; 16 } 17 //随机生成六位数 18 private static String getStringRandom(int length) { 19 20 String val = ""; 21 Random random = new Random(); 22 23 //参数length,表示生成几位随机数 24 for(int i = 0; i < length; i++) { 25 26 String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; 27 //输出字母还是数字 28 if( "char".equalsIgnoreCase(charOrNum) ) { 29 //输出是大写字母还是小写字母 30 int temp = random.nextInt(2) % 2 == 0 ? 65 : 97; 31 val += (char)(random.nextInt(26) + temp); 32 } else if( "num".equalsIgnoreCase(charOrNum) ) { 33 val += String.valueOf(random.nextInt(10)); 34 } 35 } 36 return val; 37 }
时间: 2024-10-04 03:39:19