IDUtils

/**
 * 1::圖片名生成
 * 2:商品ID生成
 * @author Administrator
 *
 */
public class IDUtils {
//圖片名生成
public static String getImageName(){
//取當前時間長整型
long currentTimeMillis = System.currentTimeMillis();
//加上三位隨機數
Random random=new Random();
int nextInt = random.nextInt(999);
//如果不足三位 前面補0
String str=currentTimeMillis+String.format("%03d", nextInt);
return str;
}

//商品ID生成
public static long getItemId() {
//取當前時間長整形
long currentTimeMillis = System.currentTimeMillis();
//加上三位隨機數
Random random=new Random();
int nextInt = random.nextInt(999);
String str=currentTimeMillis+String.format("%03d", nextInt);
long id=new Long(str);
return id;
}
//測試
public static void main(String[] args) {
String imageName = IDUtils.getImageName();
System.err.print("商品圖片名稱=="+imageName+"\r\n");
long itemId = IDUtils.getItemId();
System.out.println("商品ID==="+itemId);
}

}

測試結果:


發佈了66 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章