| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package cn.com.lzt.tools;
- import java.text.Format;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.jeecgframework.core.common.exception.BusinessException;
- import org.jeecgframework.core.util.DateUtils;
- import org.jeecgframework.core.util.StringUtil;
- /**
- * @Description: 订单号生成工具类
- * @author zhijia.wang
- * @date 2017-6-9下午2:24:40
- */
- public class OrderNumTools {
-
- private static Format sdf = new SimpleDateFormat("yyMMddHHmmssSSS");
- //流水号位数
- private static int numLength = 3;
- public static String generate18OrderNum() {
- String orderNum = "";
- Date now = new Date();
- String nowStr = sdf.format(now);
- String ranNumStr = String.valueOf(SaltUtil.randon3Num());
- orderNum = nowStr + ranNumStr;
- return orderNum;
- }
- public static void main(String[] args) {
- String orderNum = OrderNumTools.generate18OrderNum();
- System.out.println("orderNum:" + orderNum);
- }
-
- /**
- * prefix:前缀固定字母,长度必须= 2
- *
- * */
- public static String generateNextBillCode(String localMaxCode,String prefix) {
- String newcode = "";
- String date = DateUtils.formatDate(new Date(), "yyyyMMdd");
- if (localMaxCode == null || localMaxCode =="") {
- String num = getStrNum(1);
- newcode = prefix+date + num;
- } else {
- String localCodeDate = localMaxCode.substring(2,localMaxCode.length()-numLength);
- // 每一天都会从1开始,重新计算流水号
- if (date.compareTo(localCodeDate) > 0){
- String num = getStrNum(1);
- newcode = prefix+date + num;
- }else {
- String after_code = localMaxCode.substring(localMaxCode.length() - 1 - numLength,localMaxCode.length());
- Integer after_code_num = Integer.parseInt(after_code.substring(2));
-
- String nextNum = "";
- if (after_code_num == getMaxNumByLength(numLength)) {
- throw new BusinessException("超出最大单据编号,请联系管理员。"); //= getNextStrNum(0)
- } else {
- nextNum = getNextStrNum(after_code_num);
- }
- newcode = prefix + date + nextNum;
- }
- }
- return newcode;
- }
- /**
- * 将数字前面位数补零
- *
- * @param num
- * @return
- */
- private static String getNextStrNum(int num) {
- return getStrNum(getNextNum(num));
- }
- /**
- * 将数字前面位数补零
- *
- * @param num
- * @return
- */
- private static String getStrNum(int num) {
- String s = String.format("%0" + numLength + "d", num);
- return s;
- }
- /**
- * 递增获取下个数字
- *
- * @param num
- * @return
- */
- private static int getNextNum(int num) {
- num++;
- return num;
- }
- /**
- * 递增获取下个字母
- *
- * @param num
- * @return
- */
- private static char getNextZiMu(char zimu) {
- if (zimu == 'Z') {
- return 'A';
- }
- zimu++;
- return zimu;
- }
- /**
- * 根据数字位数获取最大值
- * @param length
- * @return
- */
- private static int getMaxNumByLength(int length){
- if(length==0){
- return 0;
- }
- String max_num = "";
- for (int i=0;i<length;i++){
- max_num = max_num + "9";
- }
- return Integer.parseInt(max_num);
- }
- public static String[] cutYouBianCode(String code){
- if(code==null||StringUtil.isEmpty(code)){
- return null;
- }else{
- //获取标准长度为numLength+1,截取的数量为code.length/numLength+1
- int c = code.length()/(numLength+1);
- String[] cutcode = new String[c];
- for(int i =0 ; i <c;i++){
- cutcode[i] = code.substring(0,(i+1)*(numLength+1));
- }
- return cutcode;
- }
-
- }
-
- }
|