根据时间生成随机数:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String timeStamp = simpleDateFormat.format(new Date());
timeStamp 就是时间格式:20190428150754810
要求:生成4个随机数,用到Math中random方法,这是静态方法,直接写,返回值是小数double
String mark = "";
for (int i = 0; i < 4; i++) {
int random = (int) (Math.random() * 10);
mark = mark + random;
}
这两个可以拼接起来;
String mark = "";
for (int i = 0; i < 4; i++) {
int random = (int) (Math.random() * 10);
mark = mark + random;
}
//设置文章id
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String timeStamp = simpleDateFormat.format(new Date());
String newmark=timeStamp + mark
方法二:
UUID uuid=UUID.randomUUID();
String s = uuid.toString();
System.out.println(s);
输出的s格式为:8dcdb187-1aaf-483e-8741-5067c8f5de23
它的格式是xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12),每个x是0-9或a-f范围内的一个16进制的数字;