序号号生成工具

package cn.xxxx.tools;

import java.util.Random;



public class Test {



public static void main(String[] args){
	Member member = new Member();
	member.setMemberId(SerialNumberUtil.generateId());
	System.out.println(SerialNumberUtil.generateId());
	member.setAccount("abc");
	String password = "123456";
	password = CipherUtil.MD5Encode(password);
	String sort = getCode();
	member.setPwdSalt(sort);
	member.setPwd(CipherUtil.MD5Encode(password + sort));
}

private static String getCode() {
	Random rd = new Random();
	return new StringBuilder().append(rd.nextInt(10))
			.append(rd.nextInt(10)).append(rd.nextInt(10))
			.append(rd.nextInt(10)).append(rd.nextInt(10))
			.append(rd.nextInt(10)).toString();
}
}


//0100P1K3B1XP15K8B1AB
//0100U1K3W1XJ15KOO967
//010021K331XL15LBBON8
package cn.xxxx.tools;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;


public class SerialNumberUtil
  implements Serializable
{
  private static final long serialVersionUID = 1591908593776625336L;
  private static final char[] _a = { '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 final char[] _b = { '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' };
  private static final Properties prop = new Properties();
  
  static
  {
    //InputStream inStream = SerialNumberUtil.class.getClassLoader().getResourceAsStream("XXX.properties");
    FileInputStream in=null;
	try {
		in = new FileInputStream("E:\\XXX.properties");
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}

    try
    {
      prop.load(in);
      //inStream.close();
    }
    catch (IOException localIOException) {}
  }
  
  private static ThreadLocal<StringBuilderHelper> tl = new ThreadLocal()
  {
    protected SerialNumberUtil.StringBuilderHelper initialValue()
    {
      return new SerialNumberUtil.StringBuilderHelper();
    }
  };
  private static ThreadLocal<char[]> chartl = new ThreadLocal()
  {
    protected char[] initialValue()
    {
      return new char[20];
    }
  };
  
  private static void to36String(int number, int offset, char[] buf, int charPos)
  {
    char[] chars = (char[])chartl.get();
    while (number >= 36)
    {
      buf[(--charPos)] = _b[(number % 36)];
      number /= 36;
    }
    buf[(--charPos)] = _b[number];
    System.arraycopy(buf, 0, chars, offset, buf.length);
  }
  

  
  public static String generateUUID(String str)
  {
    return UUID.fromString(str).toString().replace("-", "").toUpperCase();
  }
  
  public static String generateUUID(byte[] bytes)
  {
    return UUID.nameUUIDFromBytes(bytes).toString().replace("-", "").toUpperCase();
  }
  
  public static String generateId()
  {
    char[] chars = (char[])chartl.get();
    Calendar calendar = Calendar.getInstance();
    ThreadLocalRandom rd = ThreadLocalRandom.current();
    prop.getProperty("ServerNo").getChars(0, 2, chars, 0);
    chars[3] = 48;chars[2] = 48;
    chars[4] = _b[rd.nextInt(36)];
    to36String(calendar.get(1), 5, new char[] { '0', '0', '0' }, 3);
    chars[8] = _b[rd.nextInt(36)];
    to36String(calendar.get(6), 9, new char[] { '0', '0' }, 2);
    chars[11] = _b[rd.nextInt(36)];
    to36String(calendar.get(11) * 60 * 60 + 
      calendar.get(12) * 60 + 
      calendar.get(13), 12, 
      new char[] { '0', '0', '0', '0' }, 4);
    chars[16] = _b[rd.nextInt(36)];
    to36String(calendar.get(14), 17, new char[] { '0', '0' }, 2);
    chars[19] = _b[rd.nextInt(36)];
    return new String(chars);
  }
  
  public static String generateId(String groupId, String serialNo)
  {
    char[] chars = (char[])chartl.get();
    Calendar calendar = Calendar.getInstance();
    ThreadLocalRandom rd = ThreadLocalRandom.current();
    if (groupId.matches("[0-9a-zA-Z]{2}")) {
      groupId.toUpperCase().getChars(0, 2, chars, 0);
    } else {
      throw new IllegalArgumentException("集团组织必须为2位长!");
    }
    if (serialNo.matches("[0-9a-zA-Z]{2}")) {
      serialNo.toUpperCase().getChars(0, 2, chars, 2);
    } else {
      throw new IllegalArgumentException("单据编号必须为2位长!");
    }
    chars[4] = _b[rd.nextInt(36)];
    to36String(calendar.get(1), 5, new char[] { '0', '0', '0' }, 3);
    chars[8] = _b[rd.nextInt(36)];
    to36String(calendar.get(6), 9, new char[] { '0', '0' }, 2);
    chars[11] = _b[rd.nextInt(36)];
    to36String(calendar.get(11) * 60 * 60 + 
      calendar.get(12) * 60 + 
      calendar.get(13), 12, 
      new char[] { '0', '0', '0', '0' }, 4);
    chars[16] = _b[rd.nextInt(36)];
    to36String(calendar.get(14), 17, new char[] { '0', '0' }, 2);
    chars[19] = _b[rd.nextInt(36)];
    return new String(chars);
  }
  
 
  
  public static String to62String(long l)
  {
    StringBuilder buf = ((StringBuilderHelper)tl.get()).getStringBuilder();
    while (l >= 62L)
    {
      buf.insert(0, _a[((int)(l % 62L))]);
      l /= 62L;
    }
    buf.insert(0, _a[((int)(l % 62L))]);
    return buf.toString();
  }
  
  
  
  
  
  public static String to36String(long l)
  {
    StringBuilder buf = ((StringBuilderHelper)tl.get()).getStringBuilder();
    while (l >= 36L)
    {
      buf.insert(0, _b[((int)(l % 36L))]);
      l /= 36L;
    }
    buf.insert(0, _b[((int)(l % 36L))]);
    return buf.toString();
  }
  
 
  static class StringBuilderHelper
  {
    final StringBuilder sb;
    
    StringBuilderHelper()
    {
      this.sb = new StringBuilder();
    }
    
    StringBuilder getStringBuilder()
    {
      this.sb.setLength(0);
      return this.sb;
    }
  }
}

XXX.properties 内容

ServerNo=01

猜你喜欢

转载自blog.csdn.net/jone33/article/details/88378629