Java generates the serial number of the Application Form

Source: Internet
Author: User

Generally, the serial number of an application is required to generate the last several consecutive strings according to certain rules. below is the code used in my project to generate the serial number, in which the lock mechanism is used.

Import java. text. simpleDateFormat; import java. util. date; import org. apache. commons. lang3.StringUtils;/*** generate the serial number tool class * @ version V1.0 * @ date: 5:21:37 */public class SerialNum {private static String count = "000 "; private static String dateValue = "20131115";/*** generate the serial number */public synchronized static String getMoveOrderNo () {long No = 0; SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd "); String nowdate = sdf. format (new Date (); No = Long. parseLong (nowdate); if (! (String. valueOf (No )). equals (dateValue) {count = "000"; dateValue = String. valueOf (No);} String num = String. valueOf (No); num + = getNo (count); num = "CB" + num; return num ;} /*** get the serial number of the withdrawal ticket */public synchronized static String getMoveOrderNo (String serialNum) {String nyr = StringUtils. substring (serialNum, 2, 10); // obtain the year, month, and day String countV = StringUtils. substring (serialNum, 10); // obtain the serial number if (Integer. valueOf (countV)> Integer. valueOf (count) {dateValue = nyr; count = String. valueOf (countV);} return getMoveOrderNo ();}/*** return the number of orders for the current day + 1 */public static String getNo (String s) {String rs = s; int I = Integer. parseInt (rs); I + = 1; rs = "" + I; for (int j = rs. length (); j <3; j ++) {rs = "0" + rs;} count = rs; return rs;} public static void main (String [] args) {for (int I = 0; I <10; I ++) {System. out. println (getMoveOrderNo ());}}}

Note: The above program will not cause any problems if the server continues to run normally. If you restart the server or encounter any faults in the middle, restarting the Service may cause duplicate serial numbers, to ensure uniqueness, we need to combine the query on the database to query the last record, and then extract the serial number to call the getMoveOrderNo (String serialNum) method, in this case, the generated serial number is correct and unique.


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.