怎么生成带时间的一个随机数订单号

//
// 20201205153600
//
// 3591.411717413242
//
//
// 202012051536003591
//

public static String getRandom() {
	String result = "";//定义接收对象
	//引用时间方法
	result += TimeFormat("yyyyMMddHHmmss");
	//调用数学函数生成一个随机数
	Double rand = Math.random() * 10000;
	
	if (rand < 10) {//因为是随机数  如果说随机数小于 10  会截取索引0到1 的值  (索引是从零开始的)
		result += "000" + rand.toString().substring(0, 1);//如果是1位的话 怎么三个000拼接成功4位 
	} else if (rand < 100) {
		result += "00" + rand.toString().substring(0, 2);/如果是2位的话 怎么三个00拼接成功4位 
	} else if (rand < 1000) {
		result += "0" + rand.toString().substring(0, 3);/如果是3位的话 怎么三个0拼接成功4位 
	} else {
		result += rand.toString().substring(0, 4);
	}

// LOG.info(“18位数====={}”, result);
return result;
}

//生成当前时间
public static String TimeFormat(String timeType){
SimpleDateFormat simpleDateFormat new SimpleDateFormat(timeType);
String nowTime= simpleDateFormat.format(new Date());
return nowTime;
}

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值