package com.stylefeng.guns.modular.system.util;
|
|
|
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
import java.util.UUID;
|
|
/**
|
* 定义生成随机码的工具类
|
*/
|
public class UUIDUtil {
|
|
private int i = 1;
|
|
|
/**
|
* 定义生成原生的UUID随机码
|
* @return
|
*/
|
public static String getNativeUUID(){
|
return UUID.randomUUID().toString();
|
}
|
|
|
/**
|
* 生成32位随机码
|
* @return
|
*/
|
public static String getRandomCode(){
|
return UUIDUtil.getNativeUUID().replaceAll("-", "");
|
}
|
|
|
/**
|
* 获取给定长度的随机码
|
* @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(UUIDUtil.getRandomCode());
|
}
|
sb.append(UUIDUtil.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(UUIDUtil.getRandomCode());
|
}
|
str = sb.toString();
|
}else{
|
str = UUIDUtil.getRandomCode().substring(0, num);
|
}
|
}else{
|
return "";
|
}
|
return str;
|
}
|
|
|
/**
|
* 获取根据当前时间的字符串数据
|
* @return
|
*/
|
public synchronized static String getTimeStr(){
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmssS");
|
return simpleDateFormat.format(new Date());
|
}
|
}
|