根据时间生成唯一ID
publicclass GenerateSequenceUtil {
privatestatic final Logger logger = Logger.getLogger("log");
/** The FieldPosition. */
privatestatic final FieldPosition HELPER_POSITION = new FieldPosition(0);
/**This Format for format the data to special format. */
private final static Format dateFormat = newSimpleDateFormat("YYYYMMddHHmmssS");
/** This Format for format the number tospecial format. */
privatefinal static NumberFormat numberFormat = new DecimalFormat("0000");
/**This int is the sequence number ,the default value is 0. */
private static int seq = 0;
private static final int MAX = 9999;
/**
* 时间格式生成序列
* @return String
*/
public static synchronized StringgenerateSequenceNo() {
Calendar rightNow = Calendar.getInstance();
StringBuffer sb = new StringBuffer();
dateFormat.format(rightNow.getTime(), sb,HELPER_POSITION);
numberFormat.format(seq, sb, HELPER_POSITION);
if (seq == MAX) {
seq = 0;
}else {
seq++;
}
logger.info("THE SQUENCE IS :" +sb.toString());
return sb.toString();
}
}