时间戳/订单号

工具类——(时间戳/订单号)

public static String getOrderIdByTime() {
SimpleDateFormat sdf=new SimpleDateFormat(“yyyyMMddHHmmss”);
String newDate=sdf.format(new Date());
String result="";
Random random=new Random();
for(int i=0;i<3;i++){
result+=random.nextInt(10);
}
return newDate+result;
}
生成订单编号,编号格式(由编号类型编码+编号创建平台编码+6位日期+时间戳后4位+4位随机数组成),生成四位或者N位随机数字
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class Demo{
//测试的main方法
public static void main(String[] args) {
//获取订单编码
System.out.println(createOrderNumber(1,1));
//获取四位随机数
System.out.println(getRandNum(4));
}
/**
* 生成编号(由编号类型编码+编号创建平台编码+6位日期+时间戳后4位+4位随机数组成)
* @param numType 编号类型,1位(1-支付订单,2-退款订单)
* @param platform 编号生成平台,1位(1-PC平台,2app平台,3移动web平台)
* @return
* @throws Exception
/
public static String createOrderNumber(int numType,int platform){
//格式化日期为"yymmdd"
DateFormat format = new SimpleDateFormat(“yyMMdd”);
Date date = new Date();
StringBuffer buffer = new StringBuffer();
buffer.append(numType);
buffer.append(platform);
buffer.append(format.format(date));
buffer.append((date.getTime() + “”).substring(9));
buffer.append(getRandNum(4));
return buffer.toString();
}
/
*
* 获取四位随机数
* @param leng 随机数长度
* @return
*/
public static String getRandNum(int leng){
Random random = new Random();
StringBuffer result = new StringBuffer();
for (int i = 0; i < leng; i++) {
result.append(random.nextInt(10));
}
if(result.length()>0){
return result.toString();
}
return null;
}
}

作者:故事与她
来源:CSDN
原文:https://blog.csdn.net/qq_36410795/article/details/78790073
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/weixin_42097481/article/details/83536484