OrderNumTools.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package cn.com.lzt.tools;
  2. import java.text.Format;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import org.jeecgframework.core.common.exception.BusinessException;
  6. import org.jeecgframework.core.util.DateUtils;
  7. import org.jeecgframework.core.util.StringUtil;
  8. /**
  9. * @Description: 订单号生成工具类
  10. * @author zhijia.wang
  11. * @date 2017-6-9下午2:24:40
  12. */
  13. public class OrderNumTools {
  14. private static Format sdf = new SimpleDateFormat("yyMMddHHmmssSSS");
  15. //流水号位数
  16. private static int numLength = 3;
  17. public static String generate18OrderNum() {
  18. String orderNum = "";
  19. Date now = new Date();
  20. String nowStr = sdf.format(now);
  21. String ranNumStr = String.valueOf(SaltUtil.randon3Num());
  22. orderNum = nowStr + ranNumStr;
  23. return orderNum;
  24. }
  25. public static void main(String[] args) {
  26. String orderNum = OrderNumTools.generate18OrderNum();
  27. System.out.println("orderNum:" + orderNum);
  28. }
  29. /**
  30. * prefix:前缀固定字母,长度必须= 2
  31. *
  32. * */
  33. public static String generateNextBillCode(String localMaxCode,String prefix) {
  34. String newcode = "";
  35. String date = DateUtils.formatDate(new Date(), "yyyyMMdd");
  36. if (localMaxCode == null || localMaxCode =="") {
  37. String num = getStrNum(1);
  38. newcode = prefix+date + num;
  39. } else {
  40. String localCodeDate = localMaxCode.substring(2,localMaxCode.length()-numLength);
  41. // 每一天都会从1开始,重新计算流水号
  42. if (date.compareTo(localCodeDate) > 0){
  43. String num = getStrNum(1);
  44. newcode = prefix+date + num;
  45. }else {
  46. String after_code = localMaxCode.substring(localMaxCode.length() - 1 - numLength,localMaxCode.length());
  47. Integer after_code_num = Integer.parseInt(after_code.substring(2));
  48. String nextNum = "";
  49. if (after_code_num == getMaxNumByLength(numLength)) {
  50. throw new BusinessException("超出最大单据编号,请联系管理员。"); //= getNextStrNum(0)
  51. } else {
  52. nextNum = getNextStrNum(after_code_num);
  53. }
  54. newcode = prefix + date + nextNum;
  55. }
  56. }
  57. return newcode;
  58. }
  59. /**
  60. * 将数字前面位数补零
  61. *
  62. * @param num
  63. * @return
  64. */
  65. private static String getNextStrNum(int num) {
  66. return getStrNum(getNextNum(num));
  67. }
  68. /**
  69. * 将数字前面位数补零
  70. *
  71. * @param num
  72. * @return
  73. */
  74. private static String getStrNum(int num) {
  75. String s = String.format("%0" + numLength + "d", num);
  76. return s;
  77. }
  78. /**
  79. * 递增获取下个数字
  80. *
  81. * @param num
  82. * @return
  83. */
  84. private static int getNextNum(int num) {
  85. num++;
  86. return num;
  87. }
  88. /**
  89. * 递增获取下个字母
  90. *
  91. * @param num
  92. * @return
  93. */
  94. private static char getNextZiMu(char zimu) {
  95. if (zimu == 'Z') {
  96. return 'A';
  97. }
  98. zimu++;
  99. return zimu;
  100. }
  101. /**
  102. * 根据数字位数获取最大值
  103. * @param length
  104. * @return
  105. */
  106. private static int getMaxNumByLength(int length){
  107. if(length==0){
  108. return 0;
  109. }
  110. String max_num = "";
  111. for (int i=0;i<length;i++){
  112. max_num = max_num + "9";
  113. }
  114. return Integer.parseInt(max_num);
  115. }
  116. public static String[] cutYouBianCode(String code){
  117. if(code==null||StringUtil.isEmpty(code)){
  118. return null;
  119. }else{
  120. //获取标准长度为numLength+1,截取的数量为code.length/numLength+1
  121. int c = code.length()/(numLength+1);
  122. String[] cutcode = new String[c];
  123. for(int i =0 ; i <c;i++){
  124. cutcode[i] = code.substring(0,(i+1)*(numLength+1));
  125. }
  126. return cutcode;
  127. }
  128. }
  129. }