Random随机生成ID
程序员文章站
2022-07-05 23:43:12
...
[size=medium]常用UUID
public static String RandomStringId(){
// UUID uuid = UUID.randomUUID();
String s = UUID.randomUUID().toString();
//去掉“-”符号
return s.replaceAll("-", "");
}
////此处分开
package com.date.test;
import java.util.Random;
public class Hello {
static Random random=null;
private static char[] numberList=null;
public static void main(String[] str ){
System.out.println("randomString "+randomString(5));
}
static
{
random=new Random();
//参数10000是指0-9999的数
int number=random.nextInt(10000);
System.out.println("number "+number);
System.out.println("hello world");
//System.exit(5);
}
private final static String randomString(int length)
{
if(length<1){
return null;
}
numberList=("0123456789abcdefghijklmnopqrstuvwxyz" +
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
char[] chand=new char[length];
for(int i=0;i<chand.length;i++)
{
chand[i]=numberList[random.nextInt(72)];
}
System.out.println(chand);
return new String(chand);
}
}
user.setUserId(String.valueOf(System.currentTimeMillis()));
这样生成ID值,确实简单,但不知道是否永远不会重复?
/*
random=new Random(); 没有static初始化,必须new出来
多写了个静态类, 练习的时候写的,比较简单的随机生成ID
*/
另一种方法,可以独立运用
public class Order {
private static int orderNum = 0001;
public static String getOrderNo(){
long No = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String nowdate = sdf.format(new Date());
No = Long.parseLong(nowdate)*10000;//这里如果一天订单多的话可以用一万或更大
No+=getNo();
return getUserId()+No;
}
public static int getNo(){//返回当天的订单数+1
orderNum++;
return orderNum;
}
public static String getUserId(){
return "123";
}
public static String RandomStringId(){
UUID uuid = UUID.randomUUID();
return uuid.toString();
}
}
也可以user+随机数+流水号(流水号为全局递增变量) 服务器重启就生成新的随机数
public static String RandomStringId(){
// UUID uuid = UUID.randomUUID();
String s = UUID.randomUUID().toString();
//去掉“-”符号
return s.replaceAll("-", "");
}
////此处分开
package com.date.test;
import java.util.Random;
public class Hello {
static Random random=null;
private static char[] numberList=null;
public static void main(String[] str ){
System.out.println("randomString "+randomString(5));
}
static
{
random=new Random();
//参数10000是指0-9999的数
int number=random.nextInt(10000);
System.out.println("number "+number);
System.out.println("hello world");
//System.exit(5);
}
private final static String randomString(int length)
{
if(length<1){
return null;
}
numberList=("0123456789abcdefghijklmnopqrstuvwxyz" +
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
char[] chand=new char[length];
for(int i=0;i<chand.length;i++)
{
chand[i]=numberList[random.nextInt(72)];
}
System.out.println(chand);
return new String(chand);
}
}
user.setUserId(String.valueOf(System.currentTimeMillis()));
这样生成ID值,确实简单,但不知道是否永远不会重复?
/*
random=new Random(); 没有static初始化,必须new出来
多写了个静态类, 练习的时候写的,比较简单的随机生成ID
*/
另一种方法,可以独立运用
public class Order {
private static int orderNum = 0001;
public static String getOrderNo(){
long No = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String nowdate = sdf.format(new Date());
No = Long.parseLong(nowdate)*10000;//这里如果一天订单多的话可以用一万或更大
No+=getNo();
return getUserId()+No;
}
public static int getNo(){//返回当天的订单数+1
orderNum++;
return orderNum;
}
public static String getUserId(){
return "123";
}
public static String RandomStringId(){
UUID uuid = UUID.randomUUID();
return uuid.toString();
}
}
也可以user+随机数+流水号(流水号为全局递增变量) 服务器重启就生成新的随机数
上一篇: JAVA简单抽象实现方法
下一篇: JS ES6箭头函数