1. 程式人生 > >根據時間生成訂單

根據時間生成訂單

package util;

import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * 生成訂單Id
 *
 */
public class OrderInfoIdUtil {

    public static String getOrderInfoId(){
         Random rd=new Random();
            int m1=rd.nextInt(24);//生成0-23的隨機數
            int m2=rd.nextInt(24);
            String s="abcdeghijklmnopqrstuvwxyz"
; char rdString1=s.charAt(m1);//根據隨機的索引獲得隨機的字元 char rdString2=s.charAt(m2); String ss = String.valueOf(9999-(System.currentTimeMillis()/1000000)%10000); while(ss.length()<4){ ss = "0"+ss; } String u0 = String.valueOf(ss.charAt
(0)); String u1 = String.valueOf(ss.charAt(1)); String u2 = String.valueOf(ss.charAt(2)); String u3 = String.valueOf(ss.charAt(3)); int iu1 = Integer.parseInt(u1); char rdStringiu1=s.charAt(iu1); int iu2 = Integer.parseInt(u2); char rdStringiu2=s.charAt
(iu2+11); StringBuffer s0 = new StringBuffer() ; s0.append(String.valueOf(rdStringiu1)); s0.append(String.valueOf(rdStringiu2)); s0.append(u0); s0.append(u3); s0.append(String.valueOf(rdString1)); s0.append(String.valueOf(rdString2)); s0.append(String.valueOf(System.currentTimeMillis()%1000000)); System.out.println(s0); return s0.toString(); } public static String getOrderInfId(){ String sa = "SO"; Date d = new Date(); String u = DateUtil.getCurDateString(d); u = u.replace("-", ""); sa += u; RedisUtils rs = RedisUtils.getInstance(); rs.setString("orderinfo", 50); if(rs.getString("orderinfoday")==null){ rs.setString("orderinfoday", u); rs.setString("orderinfo", 1); }else{ if(!rs.getString("orderinfoday").equals(u)){ rs.setString("orderinfoday", u); rs.setString("orderinfo", 1); }else{ rs.incrString("orderinfo"); } } String uu = rs.getString("orderinfo"); while(uu.length()<3){ uu = "0"+uu; } return sa+uu; } public static void main(String[] args){ String i = getOrderInfId(); System.out.println(i); String uuid=UUID.randomUUID().toString(); System.out.println(uuid); } }