12 Summary of Unique Order Number Generation Methods

1: Timestamp is the number of milliseconds from the current time to 1970.
You have to have 12 digits:
new Date().getTime()-1300000000000L
This number is at least 100 years old or 12 digits. And it will not be repeated.



Timestamps will be repeated with high concurrency. You should generate them by adding 1 each time you get them. Use lock objects or declare methods as synchronized to ensure that concurrency will not be repeated. If you have a useful database, you can also use the sequence provided by the database to help you generate

and extract a method.

This is usually a serial number, or a character plus a serial number

. Java should use UUID.
Or SEQ on the database implements



Calendar calendar = Calendar.getInstance();
System.out.println("Now: "+calendar.getTime().getTime());
calendar.add(Calendar.YEAR, 10);
System.out.println("After 10 years: "+calendar.getTime().getTime());

Output:

Now: 1404208483914

10 years later: 1719827683914

If you use a timestamp and intercept the last 12 digits, not the first one, there will be no major problems in 18 years, but in this case , your method of generating IDs must be synchronized to avoid the same IDs that may appear in high concurrency (theoretically it is very possible, although the probability of generating multiple orders within 1 millisecond is very small)





package com.tangkuo.utils;

import java.util.Date;
import java.util.UUID;

public class TestNums {
public static void main(String[] args) {
long time = new Date().getTime()-1300000000000L;
System.out.println("=" + time);

System.out.println("System.currentTimeMillis()1 = " + System.currentTimeMillis());
System.out.println("UUID.randomUUID() = " + UUID.randomUUID());
System.out.println("System.currentTimeMillis()2 = " + System.currentTimeMillis());

}

}




package com.iec.app.utils;


import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class RandomId {
    private Random random;
    private String table;
    private static final int radLength =15;
    private static final String[] radArr = new String[]{"A","B","C","D","E","F","G","H","I","G","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
   
   
    public RandomId() {
        random = new Random();
        table = "0123456789";
    }
    public String randomId(long id) {
        String ret = null,num = String.format("%05d", id);
        int key = random.nextInt(10),seed = random.nextInt(100);
        Caesar caesar = new Caesar(table, seed);
        num = caesar.encode(key, num);
        ret = num  + String.format("%01d", key)  + String.format("%02d", seed);
       
        // 加入生成的随机字符
       /* int letterSize = radLength - ret.length();
        for(int i=0;i<letterSize;i++){
        String letter = radArr[ random.nextInt(radArr.length)];
        int index = random.nextInt(ret.length());
        ret = ret.substring(0, index) + letter + ret.substring(index, ret.length());
        }*/
       
        return ret;
    }
   
   
    public static void main(String[] args) {
        RandomId r = new RandomId();
        System.out.println(r.randomId(12));
    }
}



package com.iec.app.utils;


/**
*
* @author
*
*/
public class Caesar {
    private String table;
    private int seedA = 1103515245;
    private int seedB = 12345;
   
    public Caesar(String table, int seed) {
        this.table = chaos(table, seed, table.length());
    }
    public Caesar(String table) {
        this(table, 11);
    }
    public Caesar() {
        this(11);
    }
    public Caesar(int seed) {
        this("ABCDEFGHIJKLMNOPQRSTUVWXYZ", seed);
    }
    public char dict(int i, boolean reverse) {
        int s = table.length(), index = reverse ? s - i : i;
        return table.charAt(index);
    }
    public int dict(char c,  boolean reverse) {
        int s = table.length(), index = table.indexOf(c);
        return reverse ? s - index : index;
    }
    public int seed(int seed) {
        long temp = seed;
        return (int)((temp * seedA + seedB) & 0x7fffffffL);
    }

    public String chaos(String data, int seed, int cnt) {
        StringBuffer buf = new StringBuffer(data);
        char tmp; int a, b, r = data.length();
        for (int i = 0; i < cnt; i += 1) {
            seed = seed(seed); a = seed % r;
            seed = seed(seed); b = seed % r;
            tmp = buf.charAt(a);
            buf.setCharAt(a, buf.charAt(b));
            buf.setCharAt(b, tmp);
        }
        return buf.toString();
    }

    public String crypto(boolean reverse,
                         int key, String text) {
        String ret = null;
        StringBuilder buf = new StringBuilder();
        int m, s = table.length(), e = text.length();

        for(int i = 0; i < e; i += 1) {
            m = dict(text.charAt(i), reverse);
            if (m < 0) break;
            m = m + key + i;
            buf.append(dict(m % s, reverse));
        }
        if (buf.length() == e)
            ret = buf.toString();
        return ret;
    }
    public String encode(int key, String text) {
        return crypto(false, key, text);
       
    }
    public String decode(int key, String text) {
        return crypto(true , key, text);
    }
   
    public static void main(String[] args) {
        Caesar caesar = new Caesar();
        String data = caesar.encode(32, "APPLE");
        caesar.decode(32, data);
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326680446&siteId=291194637