Java Generation request Serial number implementation method _java

Source: Internet
Author: User

Copy Code code as follows:

Import Java.text.SimpleDateFormat;
Import Java.util.Date;

Import Org.apache.commons.lang3.StringUtils;

/**
* Produces the serial Number tool class
* @version V1.0
* @date: 2013-11-16 5:21:37
*/

public class Serialnum {

private static String count = "000";
private static String DateValue = "20131115";

 /**
  * produces 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 = ";
   datevalue = string.valueof (No);
  }
  string num = string.valueof (No);
  num + + getno (count);
  num = "CB" + num;
  return num;
 }

/**
* Get the withdrawal order serial number
*/
Public synchronized static string Getmoveorderno (String serialnum) {
String nyr = stringutils.substring (Serialnum, 2, 10); Get month and day string
String Countv = stringutils.substring (Serialnum, 10); Get the serial number
if (integer.valueof (COUNTV) > Integer.valueof (count)) {
DateValue = Nyr;
Count = string.valueof (COUNTV);
}
return Getmoveorderno ();
}

/**
* Return the order number of the 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 < i++) {
System.out.println (Getmoveorderno ());
}
}

}

Note: The above program if the server has been able to run normally will not be a problem, if the reboot of the server or in the middle of what failure to restart the service may cause duplicate serial number, in order to be able to guarantee the only, we need to cooperate with the database query, query the last record, and then take out the serial number in the call The Getmoveorderno (String serialnum) method ensures that the sequence number generated 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.