import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
public class PayCenterServiceImpl {
private static volatile AtomicInteger ATOM = new AtomicInteger(999);
private static Random random = new Random();
public String getNewOrderNo() {
synchronized (PayCenterServiceImpl.class) {
StringBuilder builder = new StringBuilder();
builder.append(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")));
int num = ATOM.incrementAndGet();
if (num > 9999) {
ATOM.getAndSet(1000);
num = 1000;
}
builder.append(num);
builder.append(randomInt(10, 99));
return builder.toString();
}
}
private static int randomInt(int min, int max) {
return random.nextInt(max) % (max - min + 1) + min;
}
}
不积跬步,无以至千里;不积小流,无以成江海。