package com.stylefeng.guns.modular.system.util; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; /** * 订单处理工具类 * @author taonb * @createDate 2016年5月27日 * @version 1.0 */ public class OrderUtils { /* 订单号 */ private static long orderNum = 0l; /* 日期 */ private static String date; /** * 定义生成原生的UUID随机码 * @return */ public static String getNativeUUID(){ return UUID.randomUUID().toString(); } /** * 生成32位随机码 * @return */ public static String getRandomCode(){ return OrderUtils.getNativeUUID().replaceAll("-", ""); } /** * 生成不重复的订单号 【纯数字】 * @return */ public static synchronized String getOrderNo() { String str = new SimpleDateFormat("yyMMddHHmmss").format(new Date()); if (date == null || !date.equals(str)) { date = str; orderNum = 0l; } orderNum++; long orderNo = Long.parseLong((date)) * 1000000; orderNo += orderNum; return orderNo + ""; } /** * 生成不重复的订单号 【含前缀】 * @param prefix * @return */ public static synchronized String getOrderNoForPrefix(String prefix) { return prefix + getOrderNo(); } /** * 生成不重复的订单号 【含后缀】 * @param suffix * @return */ public static synchronized String getOrderNoForSuffix(String suffix) { return getOrderNo() + suffix; } /** * 获取给定长度的随机码 * @param num * @return * @throws Exception */ public static String getRandomCode(Integer num) { String str = null; if(0 < num){ if(num % 32 > 0){ Integer s = num / 32; Integer l = num % 32; StringBuffer sb = new StringBuffer(); for(int i = 0; i < s; i++){ sb.append(OrderUtils.getRandomCode()); } sb.append(OrderUtils.getRandomCode().substring(0, l)); str = sb.toString(); }else if(num % 32 == 0){ Integer s = num / 32; StringBuffer sb = new StringBuffer(); for(int i = 0; i < s; i++){ sb.append(OrderUtils.getRandomCode()); } str = sb.toString(); }else{ str = OrderUtils.getRandomCode().substring(0, num); } } return str; } public static void main(String[] args) { System.out.println(getOrderNo()); } }