ae8a183f4ec57493da3382c4ad130a37b63d8ce7.svn-base 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package com.chinacreator.process.util;
  2. import org.apache.commons.lang.time.DateFormatUtils;
  3. import java.security.SecureRandom;
  4. import java.util.Date;
  5. import java.util.UUID;
  6. /**
  7. *
  8. * ClassName: IdGenerateUtil
  9. * @Description: 封装各种生成唯一性ID算法的工具类.
  10. * @author zengzhibin
  11. * @date 2017年9月25日
  12. */
  13. public class IdGenerateUtil {
  14. private static SecureRandom random = new SecureRandom();
  15. /**
  16. * 封装JDK自带的UUID, 通过Random数字生成, 中间有-分割.
  17. */
  18. public static String uuid() {
  19. return UUID.randomUUID().toString();
  20. }
  21. /**
  22. * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
  23. */
  24. public static String uuid2() {
  25. return UUID.randomUUID().toString().replaceAll("-", "");
  26. }
  27. /**
  28. * 使用SecureRandom随机生成Long.
  29. */
  30. public static long randomLong() {
  31. return Math.abs(random.nextLong());
  32. }
  33. public static String uuid3(){
  34. return uuid3(null,3);
  35. }
  36. public static String uuid3(String datepattern,int uuidlength){
  37. if(datepattern==null){
  38. datepattern="yyyyMMddHHmmssSSS";
  39. }
  40. String str= DateFormatUtils.format(new Date(), datepattern);
  41. if(uuidlength>0){
  42. str=str+UUID.randomUUID().toString().replaceAll("-", "").substring(0, uuidlength);
  43. }
  44. return str;
  45. }
  46. public static String uuid6(){
  47. String datepattern="yyyyMMddHHmmssSSS";
  48. String str=DateFormatUtils.format(new Date(), datepattern);
  49. str=str+(int)((Math.random()*9+1)*100000);
  50. return str;
  51. }
  52. }