package com.ezubo.global.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;
/**
* 利用原子类生成序列号 不适合分布式场景
* Created by on 2015-12-29.
*/
public class Generators2 {
private static AtomicLong no = new AtomicLong(1L);
private static final String DATE_FORMAT = "yyyyMMddHHmmss";
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat(DATE_FORMAT);
private static String forwardTime = "";
private Generators2(){
super();
}
/**
* 生成序列号
* @param prefix
* @return
*/
public static String getSeqNo(String prefix){
String pfix = prefix == null ? "" : prefix;
String currentTime = SIMPLE_DATE_FORMAT.format(new Date());
if(!currentTime.equals(forwardTime)){
no = new AtomicLong(1L);
}
String serializeNo = FormatUtil.leftpad(no.getAndIncrement(),"00000000");
synchronized (Generators.class){
forwardTime = currentTime;
}
return pfix + currentTime + serializeNo;
}
}