Generate ID Template: DD/dd + 6-bit self-increment code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.