一个简易的流水账号实现方式(java)

private static Hashtable seq = new Hashtable();
 public synchronized static String getOrderSeq(){
  String rtv = "0000";
  String datetimeKey=new SimpleDateFormat("yyyyMMdd").format(new Date());
  if ( seq.keySet().size()==0){
   seq.put(datetimeKey, 0);
  } else {
   if ( seq.get(datetimeKey) == null){
    seq.clear();
    seq.put(datetimeKey, 0);
   } else {
    int order =seq.get(datetimeKey)+1;
    String orderStr = ""+order;
    int len= rtv.length();
    int sub=orderStr.length();
    if ( len-sub>=0){
     rtv = rtv.substring(0,len-sub);
     rtv = rtv+ orderStr;
    }
    else
     rtv = orderStr;
    seq.put(datetimeKey,order);
   }
  }
  rtv = datetimeKey +rtv;
  return rtv;
 }

你可能感兴趣的:(java,integer,string,date,null,开发随笔,算法)