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.