@Override public String generate(String bizCode) { //** 获取今天的日期:yyyyMMdd *//* String date = com.i2p.util.DateUtils.getCurentDate(); String key = "serial.number:" + date; //** 自增 *//* long sequence = this.incr(key); String seq = SequenceUtils.getSequence(sequence); StringBuilder sb = new StringBuilder(); sb.append(bizCode).append(seq); String serial = sb.toString(); return serial; }
package com.i2p.util; public class SequenceUtils { static final int DEFAULT_LENGTH = 4; public static String getSequence(long seq) { String str = String.valueOf(seq); int len = str.length(); if (len >= DEFAULT_LENGTH) {// 取决于业务规模,应该不会到达4 return str; } int rest = DEFAULT_LENGTH - len; StringBuilder sb = new StringBuilder(); for (int i = 0; i < rest; i++) { sb.append('0'); } sb.append(str); return sb.toString(); } }
/** * 得到系统当前日期 * "yyyyMMdd" */ public static String getCurentDate() { SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMdd"); String datetime = tempDate.format(new java.util.Date()); return datetime; }