package test.test;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AutoBatchNumber {
private final Logger logger= LoggerFactory.getLogger(this.getClass());
private static String prefix ="ZD"; // 生成前缀
private static int counter = 0; // 流水号计数器
private static String timeCheck="20230901" ; // 时间验证
public synchronized static String generate(String suffix) {
StringBuilder stringBuilder=new StringBuilder();
if(!timeCheck.contains(suffix)){
System.out.println("--0-");
counter=0;
timeCheck=suffix;
}
counter++;
//System.out.println("--1--"+String.format("%04d", counter));
return stringBuilder.append(prefix).append(suffix).append(String.format("%04d", counter)).toString();
// return prefix +time+ +suffix+String.format("%04d", counter); // 使用四位数字格式化流水号
}
public static void main(String[] args) {
String suffix =new SimpleDateFormat("yyyyMMdd").format(new Date()); // 生成后缀
//timeCheck=suffix;
// 生成10个学号
for (int i = 0; i < 10; i++) {
String studentNumber = AutoBatchNumber.generate(suffix);
System.out.println(studentNumber);
}
// String number="ZD202309060001";
// System.out.println(number.length());
// String old=number.substring(2,10);
// System.out.println("old=="+old);
// String lastChar = number.substring(10);
// System.out.println("lastChar=="+lastChar);
// Integer last=Integer.valueOf(lastChar)+1;
// System.out.println("last=="+last);
// System.out.println(String.format("%06d", 12345));
// System.out.println(StringUtils.leftPad(last.toString(),4,"0")); //000123
}
}
标签:顺序,java,String,counter,System,流水号,suffix,println,out
From: https://blog.51cto.com/u_13478207/7389198