Because of the order ID, product ID or what ID, do not want to use self-increment, but also afraid of repetition, so use the year and day seconds + 6-bit self-increment code (total 20 bits length) as the ID
Note: If your ID is long, it is important to note thatlong is the maximum length of 19 bits , if you go directly to the problem, it is recommended to change the date of the month and minute + 5 random number
Specific code:
private static int sequence = 0;private static int length = 6;/** * yyyymmddhhmmss+6 bit self-growth code (20-bit) * @author Shijing * June 2015 29th Afternoon 1:25:23 * @return */public static synchronized String getlocaltrmseqnum () {sequence = sequence >= 999999? 1:seq Uence + 1; String datetime = new SimpleDateFormat ("YYYYMMDDHHMMSS"). Format (new Date ()); String s = integer.tostring (sequence); return datetime +addleftzero (s, length);} /** * Left fill 0 * @author shijing * June 29, 2015 PM 1:24:32 * @param S * @param length * @return */public static String Addleftzero ( String s, int length) {//StringBuilder sb=new StringBuilder (); int old = S.length (); if (length > Old) {char[] c = new C har[length];char[] x = S.tochararray (), if (X.length > length) {throw new IllegalArgumentException ("Numeric value is lar GER than intended length: "+ s+" LEN "+ length);} int lim = c.length-x.length;for (int i = 0; i < Lim; i++) {c[i] = ' 0 ';} System.arraycopy (x, 0, C, Lim, X.length); return new String (c);} Return s.substring (0, length);}
Here is the result of the test:
2015070816503700001
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Generate ID Template: DD/dd + 6-bit self-increment code