//
// 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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/230975.html
標籤:java
上一篇:java中的方法遞回
下一篇:Java的資料型別和運算子
